R:从一个列中的两个dataframes中添加匹配的向量值
我有一个数据框架,该数据帧大约是这样的:
df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(0,0,0))
单词 | 频率 | 计数 |
---|---|---|
Hello | 7 | 0 |
是 | 8 | 0 |
示例 | 5 | 0 |
我要做的是从其他数据框架中添加第三列的值,这是类似的,但看起来像以下:
df2 <- cbind(c('example','hello') ,c(5,6))
单词 | 频率 |
---|---|
示例 | 5 |
Hello | 6 |
我的目标是在两个数据帧中找到第一列的匹配值(它们具有相同的列名),并将匹配值从第二个数据框架添加到第一个数据框架的第三列。
结果应该看起来像这样:
df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(6,0,5))
单词 | 频率 | 计数 |
---|---|---|
Hello | 7 | 6 |
是 | 8 | 0 |
示例 | 5 | 5 |
我到目前为止尝试的是:
df <- merge(df,df2, by = "words", all.x=TRUE)
但是,它不起作用。
我可以帮助您了解如何完成。任何帮助将受到欢迎。
I have a data frame which is configured roughly like this:
df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(0,0,0))
words | frequency | count |
---|---|---|
hello | 7 | 0 |
yes | 8 | 0 |
example | 5 | 0 |
What I'm trying to do is add values to the third column from a different data frame, which is similiar but looks like this:
df2 <- cbind(c('example','hello') ,c(5,6))
words | frequency |
---|---|
example | 5 |
hello | 6 |
My goal is to find matching values for the first column in both data frames (they have the same column name) and add matching values from the second data frame to the third column of the first data frame.
The result should look like this:
df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(6,0,5))
words | frequency | count |
---|---|---|
hello | 7 | 6 |
yes | 8 | 0 |
example | 5 | 5 |
What I've tried so far is:
df <- merge(df,df2, by = "words", all.x=TRUE)
However, it doesn't work.
I could use some help understanding how could it be done. Any help will be welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个“更新加入”。我最喜欢的方法是在
dplyr
中:在基本r中,您可以做类似的事情:
This is an "update join". My favorite way to do it is in
dplyr
:In base R you could do the same thing like this:
这是
data.table
的选项:output
或使用
base r r:中的
在base r:或其他选项中使用
tidyverse
使用left_join
和cocece
:数据
Here is an option with
data.table
:Output
Or using
match
in base R:Or another option with
tidyverse
usingleft_join
andcoalesce
:Data