Reputation: 29
I have a list data
test1 <- list( c(ID='a',VALUE='va1'),
c(ID='a',VALUE='va2'),
c(ID='b',VALUE='vb1'),
c(ID='c',VALUE='vc1'),
c(ID='c',VALUE='vc2'),
c(ID='c',VALUE='vc3'))
And, I would like to split and merge to 3 dataframes with ID names,
please let me know how to store each data into new dataframe which has a name with thier ID.
| ID | VALUE |
| a | va1 va2 |
| ID | VALUE |
| b | vb2 |
| ID | VALUE |
| c | vc1 vc2 vc3 |
Thanks in advance
Upvotes: 0
Views: 65
Reputation: 28441
In base R
we can aggregate VALUE
after binding the list together, then split.
a <- aggregate(VALUE ~ ., do.call(rbind, test1), toString)
split(a, a$ID)
# $a
# ID VALUE
# 1 a va1, va2
#
# $b
# ID VALUE
# 2 b vb1
#
# $c
# ID VALUE
# 3 c vc1, vc2, vc3
To access each data frame, you can use:
s <- split(a, a$ID)
s$a
# ID VALUE
#1 a va1, va2
#or
s[[1]]
# ID VALUE
#1 a va1, va2
Upvotes: 3