【发布时间】:2014-08-28 16:44:24
【问题描述】:
考虑以下数据框A
A <- data.frame(ID = c(1,1,1,2,2,2), num = c(6,2,8,3,3,1))
对于A,我想拆分ID,然后计算num 的差值。可以(几乎)获得所需的结果
do.call(rbind, Map(function(x) { x$new <- c(diff(x$num), NA); x },
split(A, A$ID)))
# ID num new
# 1.1 1 6 -4
# 1.2 1 2 6
# 1.3 1 8 NA
# 2.4 2 3 0
# 2.5 2 3 -2
# 2.6 2 1 NA
do.call(rbind, ...) 在 R 用户中广受欢迎已不是什么秘密。但是在?Map 页面(Reduce、Filter 等)上使用更高阶的函数式编程函数,我认为可能有一些我不知道的东西可以替代do.call(rbind, ...),这也将重置过程中的行名。我尝试了以下方法。
> Reduce(function(x) { x$new <- c(diff(x$num), NA); x }, Map, split(A, A$ID))
# Error in f(init, x[[i]]) : unused argument (x[[i]])
> Reduce(function(x) { x$new <- c(diff(x$num), NA); x }, split(A, A$ID))
# Error in f(init, x[[i]]) : unused argument (x[[i]])
> Reduce(Map(function(x) { x$new <- c(diff(x$num), NA); x }, split(A, A$ID)))
# Error in Reduce(Map(function(x) { :
# argument "x" is missing, with no default
得到我想要的确切结果
> M <- do.call(rbind, Map(function(x) { x$new <- c(diff(x$num), NA); x },
split(A, A$ID)))
> rownames(M) <- NULL
> M
# ID num new
# 1 1 6 -4
# 2 1 2 6
# 3 1 8 NA
# 4 2 3 0
# 5 2 3 -2
# 6 2 1 NA
有没有高阶函数可以代替do.call(rbind, ...),同时合并rownames(x) <- NULL?
注意:我真的在寻找与?Map 相关的答案,但我对其他人持开放态度。
【问题讨论】:
-
你可以
Reduce(rbind, Map...)但为什么不直接使用ave或aggregate(或在它们上包装一个函数)隐藏split、lapply和_bind? -
@alexis_laz -
Reduce(rbind, Map...)是我正在寻找或在这个问题中的具体答案。 -
你们中的一个人应该将其发布为答案。
-
我希望@alexis_laz 发布它。这不是我的答案。他应该得到荣誉。
标签: r