英文:
Retrieve several values using an equivalent to the 'match' function
问题
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'
英文:
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'
答案1
得分: 3
只返回翻译好的部分:
匹配仅返回第一个匹配项。
在您的情况下,merge
将提供所需的结果。
英文:
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
答案2
得分: 2
使用%in%
:
df2[df2$person %in% df1$person, ]
# 或者不使用列名,比较第一列
# df2[df2[[1]] %in% df1[[1]], ]
# person other
# 2 j b
# 4 e f
# 5 j l
# 6 e m
# 7 j n
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论