简体   繁体   中英

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

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?

Does this help?

goalsMenu <- paste("Name", 1:40, sep="")
output <- as.data.frame(matrix(rep(0, 5 + length(goalsMenu)), nrow=1))
names(output) <- c("analysis", "patient", "date", goalsMenu, "CR1", "CR2")

Basically, I create a data.frame output with the number of columns first and name those columns in the next step. However, be aware about mdsumner's comment! This way, all columns are of class numeric . You can deal with that later though: change the class of columns in data.frame

If you can fill the frame with (some) data first, then you can just assign to names(). Otherwise, you'll have to make the list first (and then later convert to data.frame):

col.names <- LETTERS[1:10]  # Example column names
data <- vector("list", length(col.names))
names(data) <- col.names
print(str(data))            # Inspect the structure

Hope this helps

for (k in c(1:length(names_array))) {
   #Let's make a blank column, that's the length of the data frame that I'm 
   #going to attach it to:

   temp_col<-rep(NA, nrow(my_df))

   # now here's our 2nd loop
   for(i in c(1:nrow(my_df))) {
      #process the col with some stuff
       temp_col[i] <- i
    } 

    # now we're going to attach the column to the last column in the data 
    #frame
    my_df$temp_col<-temp_col

    # now we're going to assign the name from a vector 
    # we do this by looking at the length of the names
    # array, and use that as the index
    names(my_df)[length(names(my_df))]<-names_array[k]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM