Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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/4/r/78.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/0/jpa/2.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
List 改进用于折叠data.frames列表的代码_List_R_Dataframe - Fatal编程技术网

List 改进用于折叠data.frames列表的代码

List 改进用于折叠data.frames列表的代码,list,r,dataframe,List,R,Dataframe,亲爱的StackOverFlowers(简称鲜花) 我有一个data.frames(walk.sample)列表,我想将其折叠成一个(巨大的)data.frame。在折叠时,我想标记(添加另一列)哪些行来自列表的哪个元素。这就是我到目前为止得到的 这是需要折叠/堆叠的data.frame > walk.sample [[1]] walker x y 1073 3 228.8756 -726.9198 1086 3 226.7393

亲爱的StackOverFlowers(简称鲜花)

我有一个data.frames(walk.sample)列表,我想将其折叠成一个(巨大的)data.frame。在折叠时,我想标记(添加另一列)哪些行来自列表的哪个元素。这就是我到目前为止得到的

这是需要折叠/堆叠的data.frame

> walk.sample
[[1]]
     walker        x         y
1073      3 228.8756 -726.9198
1086      3 226.7393 -722.5561
1081      3 219.8005 -728.3990
1089      3 225.2239 -727.7422
1032      3 233.1753 -731.5526

[[2]]
     walker        x         y
1008      3 205.9104 -775.7488
1022      3 208.3638 -723.8616
1072      3 233.8807 -718.0974
1064      3 217.0028 -689.7917
1026      3 234.1824 -723.7423

[[3]]
[1] 3

[[4]]
     walker        x         y
546       2 629.9041  831.0852
524       2 627.8698  873.3774
578       2 572.3312  838.7587
513       2 633.0598  871.7559
538       2 636.3088  836.6325
1079      3 206.3683 -729.6257
1095      3 239.9884 -748.2637
1005      3 197.2960 -780.4704
1045      3 245.1900 -694.3566
1026      3 234.1824 -723.7423
我编写了一个函数来添加一列,该列指示行来自哪个元素,然后将其附加到现有的data.frame

collapseToDataFrame <- function(x) { # collapse list to a dataframe with a twist
    walk.df <- data.frame()
    for (i in 1:length(x)) {
        n.rows <- nrow(x[[i]])
        if (length(x[[i]])>1) {
            temp.df <- cbind(x[[i]], rep(i, n.rows))
            names(temp.df) <- c("walker", "x", "y", "session")
            walk.df <- rbind(walk.df, temp.df)
        } else {
            cat("Empty list", "\n")
        }
    }
    return(walk.df)
}


> collapseToDataFrame(walk.sample)
Empty list 
Empty list 
     walker         x          y session
3         1 -604.5055 -123.18759       1
60        1 -562.0078  -61.24912       1
84        1 -594.4661  -57.20730       1
9         1 -604.2893 -110.09168       1
43        1 -632.2491  -54.52548       1
1028      3  240.3905 -724.67284       1
1040      3  232.5545 -681.61225       1
1073      3  228.8756 -726.91980       1
1091      3  209.0373 -740.96173       1
1036      3  248.7123 -694.47380       1

collapseToDataFrame我并不认为这是最优雅的方法,但我认为它是可行的

library(plyr)

ldply(sapply(1:length(walk.sample), function(i) 
           if (length(walk.sample[[i]]) > 1)
           cbind(walk.sample[[i]],session=rep(i,nrow(walk.sample[[i]])))
      ),rbind)
编辑

在应用了马立克的贴切评论之后

do.call(rbind,lapply(1:length(walk.sample), function(i)
           if (length(walk.sample[[i]]) > 1)
           cbind(walk.sample[[i]],session=i)  ))

我想这会有用的

lengths <- sapply(walk.sample, function(x) if (is.null(nrow(x))) 0 else nrow(x))
cbind(do.call(rbind, walk.sample[lengths > 1]),
      session = rep(1:length(lengths), ifelse(lengths > 1, lengths, 0)))
1]),
session=rep(1:length(长度),ifelse(长度>1,长度,0)))

会话列的具体内容是什么?为什么要在屏幕上打印空列表?
cbind
不需要复制,只需编写
session=i
。如果没有plyr,您可以使用
do.call(rbind,sapply(…)
。嗨,gd047,我想说的是,当data.frame的行数不同时,您的解决方案将无法工作。另外,当行数相同时,结果不正确(行和列混合在一起。而且也没有列名称)。我认为用
lappy
替换
sapply
可能会有帮助。干得好,伙计们!正是医生要的。你应该使用
NROW
而不是
NROW
。对于来自问题的数据,您的解决方案将不起作用。很好,NROW是一个可能的修复方法,但我不知道当您有一行数据帧时,预期的行为是什么。我将通过执行空检查来更改它。。。