Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 526
Hannah O. Avatar asked Dec 03 '25 18:12

Hannah O.


1 Answers

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)
like image 142
joran Avatar answered Dec 06 '25 08:12

joran



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!