R:从一个列中的两个dataframes中添加匹配的向量值

发布于 2025-02-11 16:41:35 字数 2048 浏览 0 评论 0原文

我有一个数据框架,该数据帧大约是这样的:

df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(0,0,0))
单词频率计数
Hello70
80
示例50

我要做的是从其他数据框架中添加第三列的值,这是类似的,但看起来像以下:

df2 <- cbind(c('example','hello') ,c(5,6))
单词频率
示例5
Hello6

我的目标是在两个数据帧中找到第一列的匹配值(它们具有相同的列名),并将匹配值从第二个数据框架添加到第一个数据框架的第三列。

结果应该看起来像这样:

df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(6,0,5))
单词频率计数
Hello76
80
示例55

我到目前为止尝试的是:

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))
wordsfrequencycount
hello70
yes80
example50

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))
wordsfrequency
example5
hello6

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))
wordsfrequencycount
hello76
yes80
example55

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2025-02-18 16:41:35

这是一个“更新加入”。我最喜欢的方法是在dplyr中:

library(dplyr)
df %>% rows_update(rename(df2, count = frequency), by = "words")

在基本r中,您可以做类似的事情:

names(df2)[2] = "count2"
df = merge(df, df2, by = "words", all.x=TRUE)
df$count = ifelse(is.na(df$coutn2), df$count, df$count2)
df$count2 = NULL

This is an "update join". My favorite way to do it is in dplyr:

library(dplyr)
df %>% rows_update(rename(df2, count = frequency), by = "words")

In base R you could do the same thing like this:

names(df2)[2] = "count2"
df = merge(df, df2, by = "words", all.x=TRUE)
df$count = ifelse(is.na(df$coutn2), df$count, df$count2)
df$count2 = NULL
纵性 2025-02-18 16:41:35

这是data.table的选项:

library(data.table)

setDT(df)[setDT(df2), on = "words", count := i.frequency]

output

     words frequency count
    <char>     <num> <num>
1:   hello         7     6
2:     yes         8     0
3: example         5     5

或使用base r r:中的在base r:

df$count[match(df2$words, df$words)] <- df2$frequency

或其他选项中使用tidyverse使用left_joincocece

library(tidyverse)

left_join(df, df2 %>% rename(count.y = frequency), by = "words") %>%
  mutate(count = pmax(count.y, count, na.rm = T)) %>%
  select(-count.y)

数据

df <- structure(list(words = c("hello", "yes", "example"), frequency = c(7, 
8, 5), count = c(0, 0, 0)), class = "data.frame", row.names = c(NA, 
-3L))

df2 <- structure(list(words = c("example", "hello"), frequency = c(5, 6)), class = "data.frame", row.names = c(NA, 
-2L))

Here is an option with data.table:

library(data.table)

setDT(df)[setDT(df2), on = "words", count := i.frequency]

Output

     words frequency count
    <char>     <num> <num>
1:   hello         7     6
2:     yes         8     0
3: example         5     5

Or using match in base R:

df$count[match(df2$words, df$words)] <- df2$frequency

Or another option with tidyverse using left_join and coalesce:

library(tidyverse)

left_join(df, df2 %>% rename(count.y = frequency), by = "words") %>%
  mutate(count = pmax(count.y, count, na.rm = T)) %>%
  select(-count.y)

Data

df <- structure(list(words = c("hello", "yes", "example"), frequency = c(7, 
8, 5), count = c(0, 0, 0)), class = "data.frame", row.names = c(NA, 
-3L))

df2 <- structure(list(words = c("example", "hello"), frequency = c(5, 6)), class = "data.frame", row.names = c(NA, 
-2L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文