【发布时间】:2017-12-21 03:41:35
【问题描述】:
我想遍历data.table,就像purrr::map 一样。虽然我可以通过在purrr::map 中将data.frame 转换为data.table 来应用data.table 函数,但我想知道data.table 是否有内置的东西可以使用purrr::map 来消除。我问这个是因为我不确定purrr::map 在速度和所需内存方面的性能。与 data.table 在处理大型数据集时相比,我对 dplyr 的速度和内存利用率感到失望。
我研究了 stackoverflow,发现Iterate through data tables 线程上接受的答案使用了for 循环。出于性能原因,我不是 for 循环的忠实粉丝。
这里是示例数据文件:
dput(Input_File)
structure(list(Zone = c("East", "East", "East", "East", "East",
"East", "East", "West", "West", "West", "West", "West", "West",
"West"), Fiscal.Year = c(2016, 2016, 2016, 2016, 2016, 2016,
2017, 2016, 2016, 2016, 2017, 2017, 2018, 2018), Transaction.ID = c(132,
133, 134, 135, 136, 137, 171, 171, 172, 173, 175, 176, 177, 178
), L.Rev = c(3, 0, 0, 1, 0, 0, 2, 1, 1, 2, 2, 1, 2, 1), L.Qty = c(3,
0, 0, 1, 0, 0, 1, 1, 1, 2, 2, 1, 2, 1), A.Rev = c(0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 1, 0, 0), A.Qty = c(0, 0, 0, 2, 2, 3, 0,
0, 0, 0, 0, 3, 0, 0), I.Rev = c(4, 4, 4, 0, 1, 0, 3, 0, 0, 0,
1, 0, 1, 1), I.Qty = c(2, 2, 2, 0, 1, 0, 3, 0, 0, 0, 1, 0, 1,
1)), .Names = c("Zone", "Fiscal.Year", "Transaction.ID", "L.Rev",
"L.Qty", "A.Rev", "A.Qty", "I.Rev", "I.Qty"), row.names = c(NA,
14L), class = "data.frame")
这是purrr::map 和data.table 的示例代码
UZone <- unique(Input_File$Zone)
FYear <- unique(Input_File$Fiscal.Year)
a<-purrr::map(UZone, ~ dplyr::filter(Input_File, Zone == .)) %>%
purrr::map(~ data.table::as.data.table(.)) %>%
purrr::map(~ .[,.(sum = sum(L.Rev)),by=Fiscal.Year])
我不太关心输出,但我想知道有哪些替代方案可用于根据特定列迭代 data.table。如有任何想法,我将不胜感激。
【问题讨论】:
-
也许我过于简单化了,但这不只是:
b <- Input_File[, .(sum=sum(L.Rev)), by=.(Zone,Fiscal.Year)]吗?如果你真的想要,你可以split分开部分 -split(b[,-"Zone"], b$Zone) -
@Thelatemail - 这确实有帮助。有时更简单的解决方案比复杂的解决方案更好。如果您可以发布答案,我可以接受。非常感谢你的帮助。我想我掉进了兔子洞。否则,我会保留这个问题,以防万一我们得到任何其他解决方案。
标签: r dplyr data.table purrr