简体   繁体   English

根据空列将数据框列聚合到列表中

[英]Aggregate data frame column into lists based on empty column

I am trying to aggregate a specific column of a data frame into lists and concatenate the rows.我正在尝试将数据框的特定列聚合到列表中并连接行。 Thank for the help.感谢您的帮助。 Example:示例:

df <-data.frame(id=c(12,NA,NA,15,NA),name=c("John",NA,NA,"Jane",NA),grades=c(88,65,94,73,77))
> df
  id name grades
1 12 John     88
2 NA <NA>     65
3 NA <NA>     94
4 15 Jane     73
5 NA <NA>     77

I need to resulting data frame to look like this:我需要生成的数据框看起来像这样:

df1 <- data.frame(id=c(12,15),name=c("John","Jane"))
df1$grades <- list(c(88,65,94),c(73,77))
> df1
  id name     grades
1 12 John 88, 65, 94
2 15 Jane     73, 77

Fill in the NA values using na.locf from the "zoo" package, then use your preferred method of aggregation.使用“zoo”包中的na.locf填写NA值,然后使用您首选的聚合方法。 For example, here's an approach using the aggregate function in base R:例如,这是在基 R 中使用aggregate函数的方法:

library(zoo)
aggregate(grades ~ id + name, na.locf(df), c)
#   id name     grades
# 1 15 Jane     73, 77
# 2 12 John 88, 65, 94
str(.Last.value)
# 'data.frame': 2 obs. of  3 variables:
#  $ id    : chr  "15" "12"
#  $ name  : chr  "Jane" "John"
#  $ grades:List of 2
#   ..$ 1.2: chr  "73" "77"
#   ..$ 2.1: chr  "88" "65" "94"

You can also use data.tale and zoo packages (assuming grades is the third column):您还可以使用data.talezoo包(假设grades是第三列):

library(data.table)
 setDT(zoo::na.locf(df))[,.(grades= list(grades)), by=names(df[,-3])]

#    id name     grades 
# 1: 12 John 88, 65, 94 
# 2: 15 Jane     73, 77

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM