Reputation: 213
I have data.frame of two columns with different size:
type1 type2
user1 user1
user2 user4
user3 user6
user4
how to get the matches? that the result would be
user1
user4
Upvotes: 1
Views: 100
Reputation: 89057
First of all, the columns in a data.frame must all have the same length (see http://cran.r-project.org/doc/manuals/R-intro.html#Data-frames.) so I will assume the last entry in your example is just an empty string "".
What you are looking for is the "intersection" of your two columns, i.e., the elements that can be found in both columns. You can use the intersect
function for that:
df <- data.frame(type1 = c("user1", "user2", "user3", "user4"),
type2 = c("user1", "user4", "user6", ""))
df
# type1 type2
# 1 user1 user1
# 2 user2 user4
# 3 user3 user6
# 4 user4
intersect(df$type1, df$type2)
# [1] "user1" "user4"
Upvotes: 0
Reputation: 25726
See ?match
or the similar ?"%in%"
:
df$type1[df$type1 %in% df$type2]
Upvotes: 1