Eric
Eric

Reputation: 1389

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

Upvotes: 2

Views: 464

Answers (2)

M--
M--

Reputation: 29109

You can also use data.table and zoo packages (assuming grades is the third column):

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

Upvotes: 0

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193637

Fill in the NA values using na.locf from the "zoo" package, then use your preferred method of aggregation. For example, here's an approach using the aggregate function in base R:

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"

Upvotes: 3

Related Questions