1

I'm currently working in R with data.table and am trying to add a variable "data_set" to a list of data tables that holds the name of that item in the list.

For example - The list of tables is med, symp, and care and I would like to add the variable data_set to each table where in data table med, "data_set" would equal "med" and in data table symp, "data_set" would equal "symp". I know I can use lapply for this, but I'm stuck on how to store the name of each table in the "data_set" variable. I know it would go something like this....

   tables<-c("med", "symp", "care")
   tables_1<-lapply(mget(tables),function(x)x[, data_set:=…...])

How can I reference and store the name of each data table in the "data_set" variable?

1 Answer 1

3

I suggest you can use Map instead of lapply here.

med <- data.table(a=1); symp <- data.table(b=1); care <- data.table(d=1)
tables <- c("med", "symp", "care")
tables_1 <- Map(function(x, nm) copy(x)[, data_set := nm], mget(tables), tables)
tables_1
# $med
#    a data_set
# 1: 1      med
# $symp
#    b data_set
# 1: 1     symp
# $care
#    d data_set
# 1: 1     care

I use copy inferring that your use of lapply was not needing side-effect. Without copy, your global-env variables (med, symp, care) would have had the column added as well. This might be desired and/or acceptable, in which case remove copy to reduce the memory footprint.

Sign up to request clarification or add additional context in comments.

9 Comments

This should work, but I get the error- Error in mget(tables) : invalid first argument
If tables is a character vector and all of those names exist in the current environment, then it will work. (I can reproduce that error if I mis-type mget(table), which is using the function table and not the variable tables.)
It is likely referencing data.table::tables and failing.
I was able to get this to work. My mistake. I was using it on a list that resulted from a previous lapply by accident. If I use it on the original list (as listed in my question) it works! But is there a way to get it to work on a list of data tables that results from a previous lapply? Would I need to alter the list prior to plugging it in?
and also lapply(tables, function(x) get(x)[, data_set := x])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.