I have multiple data tables and all have a common column called ID. I have a vector vec that contains a set of ID values.
I would like to use lapply to subset all data tables using vec
I understand how to use lapply to subset the data tables but my question is how to assign the subsetted results back to original data tables
Here is what I tried :
tables<-c("dt1","dt2","dt3","dt4")
lapply(mget(tables),function(x)x[ID %in% vec,])
The above gives subsets of all data tables but how do I assign them back to dt1,dt2,dt3,dt4 ?
IDshould bex$IDif you didn't attach the dataset (attach is not reccommended). Also, it is better to keep it in the list rather than having many objects in the global environment. You can uselist2envif you insist. it.lst <- lapply(mget(tables),function(x)x[ID %in% vec,]); list2env(lst, envir=.GlobalEnv)- akrunother user sessions. It is generally not good to have a lot of objects in the global environment - akrun