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
We can get the
length
of each element of 'list' usinglengths
(introduced inR 3.2.0
), replicate the 'df$id',unlist
the 'list' and create a 'data.frame' with those vectors.Or we can set the names of the 'list' with the 'id' column of the dataset ('df') and use
stack
to get the long formOr a similar approach with
unnest
fromtidyr
NOTE: It is better not to name objects with a function name (in reference to 'list', 'df')