Reputation: 2597
Suppose I have 3 data frames (df1, df2, df3) that explain the color, year and make of a car. Each data frame has a column named "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.
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:
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? Any thoughts?
Upvotes: 2
Views: 23450
Reputation: 456
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.
Upvotes: 6
Reputation: 70336
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
Upvotes: 1