簡體   English   中英

R:在 lapply 內使用 data.table

[英]R: using data.table inside lapply

我有一個 data.table:

> dt
id x
1  2
2  1
2  3

要按 id 計算數據,我使用以下命令:

> dt[, .N, by = id]
id N
1  1
2  2

但是我有這些數據表的完整列表,我想對它們中的每一個都進行上述操作。 在這種情況下我怎么稱呼 lapply ? IE:

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>))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM