[英]Create a list of unique values from a column in multiple data frames in R
Suppose I have 3 data frames (df1, df2, df3) that explain the color, year and make of a car. 假设我有3个数据框(df1,df2,df3),它们说明了汽车的颜色,年份和品牌。 Each data frame has a column named "id".
每个数据帧都有一个名为“ id”的列。 Not all of the ids match in each data frame, but I want to create a list of unique ids across all three data frames.
并非每个数据框中的所有ID都匹配,但是我想在所有三个数据框中创建一个唯一ID列表。
df1 looks like this:
id color
5A black
7T green
8Q gold
4D white
df2 looks like this:
id make
5A BMW
6N Benz
8Q GM
3G Toyota
df3 looks like this:
id year
5A 2003
2B 2010
8Q 1999
4D 2007
Here's the code to make the 3 data frames: 这是制作3个数据帧的代码:
df1 <- data.frame(id=c("5A", "7T", "8Q", "4D"), color=c("black", "green", "gold", "white"))
df2 <- data.frame(id=c("5A", "6N", "8Q", "3G"), make=c("BMW", "Benz", "GM", "Toyota"))
df3 <- data.frame(id=c("5A", "2B", "8Q", "4D"), year=c(2003,2010,1999,2007))
I would like my final list to have the following values: 我希望我的最终列表具有以下值:
id
5A
7T
8Q
4D
6N
3G
2B
I tried using unique() but that only works for one vector? 我尝试使用unique(),但仅适用于一个向量? Any thoughts?
有什么想法吗?
unique(c(df1$id, df2$id, df3$id))
works for numeric values. 适用于数值。
unique(c(as.character(df1$id), as.character(df2$id), as.character(df3$id)))
returns the values as characters, and you could manipulate them back to factors if that's desired. 以字符的形式返回值,如果需要,您可以将其重新设置为因子。
This may be a way of doing it 这可能是一种方法
lst <- list(df1, df2, df3)
unique(unlist(lapply(lst, function(x) unique(x[,1]))))
[1] 5A 7T 8Q 4D 6N 3G 2B
Levels: 4D 5A 7T 8Q 3G 6N 2B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.