Matching elements of a list to a data frame using R

3.6k views Asked by At

df is a data frame that contains ids, list is a list of attributes. The first element of list contains the attributes for the first observation of df, and so on.

How can I obtain a dataframe that matches ids with attributes?

An example

set.seed(1)
df= data.frame(id=paste(rep("id",10),1:10,sep=""))
list=replicate(10,letters[sample(1:26,sample(1:3,1),replace=T)])

head(df)
# id 
# id1
# id2
# id3
# id4
# id5


head(list)
[[1]]
[1] "j"

[[2]]
[1] "x" "f"

[[3]]
[1] "y" "r" "q"

[[4]]
[1] "f"

[[5]]
[1] "r"

[[6]]
[1] "u" "m"

The first 5 observations of the resulting data frame should look like this

   id attribute
1 id1         j
2 id2         x
3 id2         f
4 id3         y
5 id3         r
1

There are 1 answers

2
akrun On BEST ANSWER

We can get the length of each element of 'list' using lengths (introduced in R 3.2.0), replicate the 'df$id', unlist the 'list' and create a 'data.frame' with those vectors.

 res <- data.frame(id=rep(df$id,lengths(list)), attribute=unlist(list))
 head(res)
 #     id attribute
 #  1 id1         j
 #  2 id2         x
 #  3 id2         f
 #  4 id3         y
 #  5 id3         r
 #  6 id3         q

Or we can set the names of the 'list' with the 'id' column of the dataset ('df') and use stack to get the long form

  stack(setNames(list, df$id))

Or a similar approach with unnest from tidyr

  library(tidyr)
  unnest(setNames(list, df$id), id)

NOTE: It is better not to name objects with a function name (in reference to 'list', 'df')