简体   繁体   English

使用R将列表元素与数据框匹配

[英]Matching elements of a list to a data frame using R

df is a data frame that contains ids, list is a list of attributes. df是一个包含ID的数据框, list是一个属性列表。 The first element of list contains the attributes for the first observation of df , and so on. list的第一个元素包含df第一次观察的属性,依此类推。

How can I obtain a dataframe that matches ids with attributes? 如何获得将ID与属性匹配的数据框?

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 结果数据帧的前5个观察结果应如下所示

   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' using lengths (introduced in R 3.2.0 ), replicate the 'df$id', unlist the 'list' and create a 'data.frame' with those vectors. 我们可以得到的length使用“列表”中的每个元素的lengths (以介绍R 3.2.0 ),复制“DF的$ id”, unlist的“名单”,并创建一个“data.frame”这些载体。

 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 或者我们可以使用数据集的“ id”列(“ df”)设置“列表”的名称,并使用stack获取长格式

  stack(setNames(list, df$id))

Or a similar approach with unnest from tidyr 或用类似的方法unnesttidyr

  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') 注意:最好不要使用函数名称来命名对象(参考“ list”,“ df”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM