Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
do.call(rbind,…)是否有更高的替代品?_R - Fatal编程技术网

do.call(rbind,…)是否有更高的替代品?

do.call(rbind,…)是否有更高的替代品?,r,R,考虑以下数据帧A A <- data.frame(ID = c(1,1,1,2,2,2), num = c(6,2,8,3,3,1)) 众所周知,do.call(rbind,…)在R用户中广受欢迎。但是有了?Map页面上的高阶函数编程函数(Reduce,Filter,等等),我想可能有一些我不知道的东西可以替代do.call(rbind,…),它也会在过程中重置行名。我试过以下方法 > Reduce(function(x) { x$new <- c(diff(x$num),

考虑以下数据帧
A

A <- data.frame(ID = c(1,1,1,2,2,2), num = c(6,2,8,3,3,1))
众所周知,
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

>Reduce(函数(x){x$new Reduce(函数(x){x$new Reduce)Map(函数(x){x$new)M可以说,这种分割-应用-组合方法是
plyr
的全部内容。它不是在基R中,而是有效的“高阶”

dplyr
版本(显然
transform
不是
dplyr
感知的:必须改用
mutate


您可以从“data.table”中查看
rbindlist

库(data.table)
rbindlist(Map)(函数(x){

x$new您可以
Reduce(rbind,Map…
),但为什么不直接使用
ave
aggregate
(或在那些隐藏
split
lappy
\u bind
?@alexis\u laz-
Reduce(rbind,Map…)的函数呢
是我正在寻找的或这个问题中的具体答案。你们中的一位应该将其作为答案发布。我希望@alexis_laz发布。这不是我的答案。他应该获得学分。你愿意解释一下最后的
[]
在做什么吗…?(其余的对我来说很有意义。)@BenBolker,这只是打印结果。这些都很好。我一直采用“如果你不必加载程序包,为什么要加载程序包”的方法。但现在,不利用所有优秀的程序包几乎是愚蠢的。也许我应该更新这个问题,给我你所有的
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
library("plyr")
ddply(A,"ID",transform,new=c(diff(num),NA))
library("dplyr")
A %>% group_by("ID") %>% 
     mutate(new=c(diff(num),NA))
library(data.table)

rbindlist(Map(function(x) { 
  x$new <- c(diff(x$num), NA)
  x}, split(A, A$ID)))
#    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
DT <- as.data.table(A)

DT[, new := c(diff(num), NA), by = ID][]
#    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