Reputation: 463
I am currently using data.table in R and am attempting to subset a list of several data tables down to one specific variable called "name". I have tried using lapply to do so, but haven't been successful. Is there something wrong with my code?
Here's what I've tried:
data_tables<-c("one", "two", "three", "four", "five")
lapply(mget(data_tables),function(x)x[, c("name")])
Upvotes: 0
Views: 367
Reputation: 887168
We can use .(
library(data.table)
lapply(mget(data_tables),function(x)x[, .(name)])
Or specify the column/columns in .SDcols
and subset the .SD
lapply(mget(data_tables),function(x)x[, .SD, .SDcols = 'name'])
Or if we want to extract as a vector, use [[
lapply(mget(data_tables),function(x) x[["name"]])
Upvotes: 3