使用等效于“match”函数来检索多个值。

huangapple go评论74阅读模式
英文:

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 &lt;- data.frame(c(&#39;j&#39;,&#39;g&#39;,&#39;e&#39;), c(5,8,3))
colnames(df1) &lt;- c(&#39;person&#39;, &#39;number&#39;)

df2 &lt;- data.frame(c(&#39;p&#39;,&#39;j&#39;,&#39;w&#39;,&#39;e&#39;,&#39;j&#39;,&#39;e&#39;,&#39;j&#39;), c(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;f&#39;,&#39;l&#39;,&#39;m&#39;,&#39;n&#39;))
colnames(df2)&lt;-c(&#39;person&#39;,&#39;other&#39;)

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

result &lt;- df2[match(df1$person, df2$&#39;person&#39;), &#39;other&#39;]

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 &#39;j&#39; and two values from &#39;e&#39;. 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 &#39;j&#39;:

df2$&#39;person&#39;==&#39;j&#39;

答案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

huangapple
  • 本文由 发表于 2023年6月12日 19:55:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456418-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定