Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve several values using an equivalent to the 'match' function

Tags:

dataframe

r

match

I have two dataframes:

df1 <- data.frame(c('j','g','e'), c(5,8,3))
colnames(df1) <- c('person', 'number')

df2 <- data.frame(c('p','j','w','e','j','e','j'), c('a','b','c','f','l','m','n'))
colnames(df2)<-c('person','other')

And I want to retrieve values from df2$'other' based on matches between df1$'person' and df2$'person'. I used the following code:

result <- df2[match(df1$person, df2$'person'), 'other']

But I only obtain the value from the first match.

Is there any easy way, if possible in 'base R', to obtain all the matches? In this case, three values from 'j' and two values from 'e'. I'm working with big dataframes so I'm looking for an economic and fast solution. When I use this I got all the entries for 'j':

df2$'person'=='j'
like image 984
Carpa Avatar asked Jun 04 '26 09:06

Carpa


2 Answers

Match will only give the first match.
In your case merge will give the desired result.

merge(df1, df2)
#  person number other
#1      e      3     f
#2      e      3     m
#3      j      5     b
#4      j      5     l
#5      j      5     n
like image 56
GKi Avatar answered Jun 06 '26 22:06

GKi


Using %in%:

df2[ df2$person %in% df1$person, ]
# or without column names, compare 1st columns
# df2[ df2[[1]] %in% df1[[1]], ]

#   person other
# 2      j     b
# 4      e     f
# 5      j     l
# 6      e     m
# 7      j     n
like image 45
zx8754 Avatar answered Jun 07 '26 00:06

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!