Hannah O.
Hannah O.

Reputation: 383

Referring to a data frame by a variable name when creating a new column in R

I have a series of ten data frames containing two columns, x and y. I want to add a new column to each data frame containing the name of the data frame. The problem I am running into is how to refer to the data frame using a variable so I can perform this task iteratively. In addition to just referring to it by the variable name, I have also tried get() as follows:

for(i in 1:10){
   name <- paste(substr(fileList, 3, 7),i, sep = "")
   assign(newName, as.data.frame(get(name)))
   get(newName)$Species = c(paste(substr(fileList, 3, 7),i, sep = ""))
}

However, I get the following error when I do so:

Error in get(newName)$Species = c(paste(substr(fileList[a], 3, 7), i,  : 
  could not find function "get<-"

Is there another way to phrase the column assignment command so that I can get around this error, or is the solution more complex?

Upvotes: 3

Views: 1463

Answers (1)

joran
joran

Reputation: 173517

Here are three different options if you put all your data frames into a named list:

df_list <- list(a = data.frame(x = 1:5),
                b = data.frame(x = 1:5))
#Option 1               
for (i in seq_along(df_list)){
    df_list[[i]][,'Species'] <- names(df_list)[i]
}

#Option 2
tmp <- do.call(rbind,df_list)
tmp$Species <- rep(names(df_list),times = sapply(df_list,nrow))
split(tmp,tmp$Species)

#Option 3
mapply(function(x,y) {x$Species <- y; x},df_list,names(df_list),SIMPLIFY = FALSE)

Upvotes: 5

Related Questions