简体   繁体   English

在R中多个数据框中的一列中创建唯一值列表

[英]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.

相关问题 获取 R 中数据框列表中单列的唯一值 - get unique values of single column in list of data frames in R 根据一列中的唯一值比较多个数据帧,并在R中的多个数据帧中查找第二列中的重叠值 - Comparing multiple data frames based on unique values in one column and finding overlapping values in second column in multiple data frames in R 如何在 R 中创建一个新列来匹配来自两个不同数据框中的多个值 - How to create a new column in R which matches multiple values from two different data frames R-从现有列值创建和命名数据框 - R - create and name data frames from existing column values R-通过唯一ID比较不同长度的数据帧中的列值 - R - Compare column values in data frames of differing lengths by unique ID 匹配数据框列表中的值R - Match values from list of data frames R rbindlist data.frames的列表列并选择唯一值 - rbindlist a list column of data.frames and select unique values 从多个数据框的列表中提取特定的列,并将它们组合成r中的新数据框 - extracting particular column from a list of multiple data frames and combine them into a new data frame in r 从单独的值列表中替换多个数据框(在列表中)列名称? - Replace multiple data frames (in a list) column names from a separate list of values? R:从多列创建基于新列的值列表 - R: Create new column based list of values from a multiple columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM