Reputation: 555
I have a data frame with the following structure:
language subID sessionID rdm
(chr) (fctr) (fctr) (chr)
1 Dutch 13602 13257 <data.frame [676,3]>
2 Dutch 13602 125354 <data.frame [676,3]>
3 Dutch 17790 19308 <data.frame [676,3]>
4 Dutch 37016 38221 <data.frame [676,3]>
5 Dutch 46830 47890 <data.frame [676,3]>
I would like to transform this data frame into a 676*n by 6 data frame - in other words, take the rdm column of data frames and expand it in to the larger data frame.
I would like to be able to do something like:
bind_rows(data_frame$rdm,.id=c(data_frame$sessionID,data_frame$subID,data_frame$language))
But, obviously, the ".id" attribute doesn't work that way. How can I get the data frame I'm after?
Upvotes: 2
Views: 1356
Reputation: 887961
We can use unnest
from library(tidyr)
library(tidyr)
unnest(df1, rdm)
# Source: local data frame [6 x 4]
# language sessionID V1 V2
# (chr) (dbl) (int) (int)
#1 Dutch 13257 1 2
#2 Dutch 13257 2 3
#3 Dutch 13257 3 4
#4 Dutch 125354 4 5
#5 Dutch 125354 5 6
#6 Dutch 125354 6 7
library(dplyr)
df1 <- data_frame(language=c('Dutch', 'Dutch'), sessionID=c(13257, 125354),
rdm= list(data.frame(V1=1:3, V2=2:4), data.frame(V1=4:6, V2=5:7)))
Upvotes: 7