我有一个data.table:
> dt
id x
1 2
2 1
2 3
要通过id计数数据,我使用此命令:
> dt[, .N, by = id]
id N
1 1
2 2
但是我有这些数据表的完整列表,我想对它们中的每一个进行以上操作。在这种情况下,我该如何称呼lapply?即:
lapply(list_of_dts, ??)
您可以在data.table的列表上使用相同的命令
library(data.table)
lapply(list_of_dt, function(dt) dt[, .N, by = id])
#[[1]]
# id N
#1: 1 1
#2: 2 2
#[[2]]
# id N
#1: 1 1
#2: 2 2
数据
list_of_dt <- list(structure(list(id = c(1L, 2L, 2L), x = c(2L, 1L, 3L)),
class = c("data.table", "data.frame"), row.names = c(NA, -3L),
.internal.selfref = <pointer: 0x10400a0e0>),
structure(list(id = c(1L, 2L, 2L), x = c(2L, 1L, 3L)), class = c("data.table",
"data.frame"), row.names = c(NA, -3L), .internal.selfref = <pointer: 0x10400a0e0>))