0

I'm trying to create a column in multiple data frames that contain the name of the dataframe. This is so when they are appended I know where they came from. This is easy to do manually for each dataframe:

# create some example data that need joining
Pb = c(5, 6, 4, 5, 7)
depth = c(1, 2, 3, 4, 5)
df1 <- data.frame(Pb, depth)
df2 <- df1 - 0.5

# add a column to each with the name of the original dataframe
df1$label <- deparse( substitute(df1) )
df2$label <- deparse( substitute(df2) )

Produces the desired output:

> df1
  Pb depth label
1  5     1   df1
2  6     2   df1
3  4     3   df1
4  5     4   df1
5  7     5   df1
> df2
   Pb depth label
1 4.5   0.5   df2
2 5.5   1.5   df2
3 3.5   2.5   df2
4 4.5   3.5   df2
5 6.5   4.5   df2

I'm trying to create a function that will accept a list of dataframes and do the same, but I'm struggling using deparse(substitute(x)) in lapply. My (not working) code below:

label_tool = function(list) {    
addlabels = function(x){ x$labels<-deparse(substitute(x)) }
list <- lapply( list, addlabels )   
return(df)
}

# example usage (see above)
list <- list( df1, df2 )
list <- label_tool(list)

I see others have managed to use deparse(substitute(x)) in lapply, but frankly, I don't understand how. Can anyone help?

Community
  • 1
  • 1
Tom Bishop
  • 53
  • 4
  • When you return `list` in your function `label_tool`, the function will do what it should. However, it probably wont be what you expected. Note that once you have created `list`, there is no way of figuring out what the original variable names of the data frames were. – Stibu Jan 27 '17 at 15:45
  • 3
    Maybe use a named list, `lapply` through the list names and output the new list: `myList <- lapply(names(myList), function(i) within(myList[[i]], {label <- i}))`. – lmo Jan 27 '17 at 15:47
  • If you use a named list then rbind'ing its components together will assign the names (and sequence numbers) as the rownames so you don't really need an extra column. Try this using built-in `BOD`: `do.call("rbind", list(BOD1 = BOD, BOD2 = BOD))` – G. Grothendieck Jan 27 '17 at 16:48
  • Thanks @Imo & @ G. Grothendieck - the lesson learnt is: use a named list. I've solved my issue with your advice. – Tom Bishop Jan 27 '17 at 18:29

0 Answers0