从列表值创建数据框

时间:2014-04-02 19:48:26

标签: r list dataframe

我有以下列表:

peter <- data.frame(year = 1:5, a = rnorm(5), b = rnorm(5))
john <- data.frame(year = 1:5, a = rnorm(5), b = rnorm(5))

myList <- list(peter, john)
names(myList) <- c("peter", "john")

myList

    $peter
    year           a         b
    1      1.01464245 0.2490931
    2      1.38054309 0.8396630
    3     -0.84094830 0.2410526
    4     -0.05567379 0.6369121
    5     -0.66412862 1.5739672

    $john
    year          a          b
    1      0.3060996 -0.4256702
    2      0.7167710 -0.6828029
    3     -0.6896138  0.6577422
    4     -1.7647412 -0.5651756
    5      0.3065734 -0.4860141

如何将myList转换为以下数据框:

   year student           a          b
1     1   peter  1.01464245  0.2490931
2     2   peter  1.38054309  0.8396630
3     3   peter -0.84094830  0.2410526
4     4   peter -0.05567379  0.6369121
5     5   peter -0.66412862  1.5739672
6     1    john  0.30609964 -0.4256702
7     2    john  0.71677097 -0.6828029
8     3    john -0.68961377  0.6577422
9     4    john -1.76474117 -0.5651756
10    5    john  0.30657340 -0.4860141

非常感谢。

4 个答案:

答案 0 :(得分:5)

library(plyr)

dat <- ldply(myList)
colnames(dat) <- c("student", "year", "a", "b")

print(dat)
##    student year           a          b
## 1    peter    1  0.03716519  0.8465317
## 2    peter    2 -1.15449127  1.5461944
## 3    peter    3  0.15933780  0.7468312
## 4    peter    4  0.91745104  0.1113958
## 5    peter    5 -0.22924789 -0.5344617
## 6     john    1  0.40790134  0.5886599
## 7     john    2 -0.88635369 -0.3596063
## 8     john    3 -1.16444277  1.1080161
## 9     john    4 -0.19082412  0.1675609
## 10    john    5  1.19066829 -0.8855810

答案 1 :(得分:3)

与Ben相似,只是有点不同。

dd <- do.call(rbind, myList)
cbind(dd[1], student = sub("[.].*", "", rownames(dd)), dd[2:3], row.names = NULL)
#    year student           a          b
# 1     1   peter -1.66983899  0.3683629
# 2     2   peter  0.25391016 -0.4999335
# 3     3   peter -0.19102468 -0.9344484
# 4     4   peter  1.72821089 -2.6148841
# 5     5   peter  0.30320439 -0.2602509
# 6     1    john -0.02447092 -0.2396401
# 7     2    john -1.57022813  1.1159078
# 8     3    john  2.82545689  0.6818537
# 9     4    john -0.11273218 -1.8000738
# 10    5    john -1.39706920  0.1647720

2015年9月16日更新我之前回答的改进:

f <- function(x, y) cbind(x[1], student = y, x[-1])
do.call(rbind, Map(f, myList, names(myList), USE.NAMES = FALSE))

答案 2 :(得分:3)

另一种选择(非常类似于Ben)

> df <- do.call(rbind, myList)
> df <- transform(df, student=sub("\\.[0-9]", "", rownames(df)))[, c("year", "student", "a", "b")]
> rownames(df)<- NULL
> df
   year student           a           b
1     1   peter -0.71040656 -0.04502772
2     2   peter  0.25688371 -0.78490447
3     3   peter -0.24669188 -1.66794194
4     4   peter -0.34754260 -0.38022652
5     5   peter -0.95161857  0.91899661
6     1    john -0.57534696  0.30115336
7     2    john  0.60796432  0.10567619
8     3    john -1.61788271 -0.64070601
9     4    john -0.05556197 -0.84970435
10    5    john  0.51940720 -1.02412879

答案 3 :(得分:2)

有点笨拙,但是:

peter <- data.frame(year = 1:5, a = rnorm(5), b = rnorm(5))
john <- data.frame(year = 1:5, a = rnorm(5), b = rnorm(5))
myList <- list(peter=peter, john=john)
do.call(rbind,
      mapply(function(student,d) { data.frame(student,d) },
            names(myList),myList,SIMPLIFY=FALSE))
  • 定义一个函数,该函数根据学生姓名和学生信息创建数据框;
  • 将其传递给mapply()(带SIMPLIFY=FALSE)以获取增强数据框列表;
  • do.call(rbind,...)合并各个部分。

可替换地:

info <- do.call(rbind,myList)
student <- rep(names(myList),sapply(myList,nrow))
data.frame(student,info)