Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an data.frame in R with dynamically assigned column names

Tags:

I need to create a data.frame that will be populated one row at a time by results of a for loop. It has 45 columns: the names of five of these are static but the remainder are read in (as a vector) from an external CSV file at run time. I'm looking for something along the lines of

goalsMenu <- read.csv("Phase 1 goalsmenu.csv", header = TRUE)
colHeads <- c("analysis","patient","date",as.vector(goalsMenu$Name),"CR")
output <- data.frame(colHeads)

however this creates a one-column data.frame with column name of colHeads.

colHeads <- list("analysis","patient","date",as.vector(goalsMenu$Name),"CR")

seems a step in the right direction but I need to "flatten" it to create the desired data.frame structure

could you advise please?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!