gpt4 book ai didi

r - 如何将单词分配给数据框中的数字

转载 作者:行者123 更新时间:2023-12-04 09:16:27 27 4
gpt4 key购买 nike

我有一个下面的数据框,其中两列有数字,我应该使用我的其他引用数据集将其替换为字符串。

数据集 1:

lhs         rhs 
32,39,6 65
39,6,65 32
14,16,26 15
16,20,4 26
16,26,33 4
53 31

数据集 2:

id   name
4 yougurt
6 coffee
14 cream chese
15 meat spreads
16 butter
20 whole milk
26 condensed milk
31 curd
32 flour
39 rolls
53 sugar
65 soda

预期输出:

lhs                                     rhs
flour, rolls, coffee soda
rolls, coffee, soda flour
cream chease, butter, condensed milk meat spreads

最佳答案

使用 的解决方案和 . dat 是最终输出。关键是用separate_rows展开lhs,然后进行两次left_join

library(dplyr)
library(tidyr)

dat <- dat1 %>%
separate_rows(lhs, convert = TRUE) %>%
left_join(dat2, by = c("lhs" = "id")) %>%
left_join(dat2, by = c("rhs" = "id")) %>%
drop_na(name.x) %>%
group_by(name.y) %>%
summarise(lhs = paste0(name.x, collapse = ", ")) %>%
ungroup() %>%
select(lhs, rhs = name.y)

dat
# # A tibble: 6 x 2
# lhs rhs
# <chr> <chr>
# 1 butter, whole milk, yougurt condensed milk
# 2 sugar curd
# 3 rolls, coffee, soda flour
# 4 cream chese, butter, condensed milk meat spreads
# 5 flour, rolls, coffee soda
# 6 butter, condensed milk yougurt

数据

dat1 <- read.table(text = "lhs         rhs 
'32,39,6' 65
'39,6,65' 32
'14,16,26' 15
'16,20,4' 26
'16,26,33' 4
53 31 ",
stringsAsFactors = FALSE, header = TRUE)

dat2 <- read.table(text = "id name
4 yougurt
6 coffee
14 'cream chese'
15 'meat spreads'
16 butter
20 'whole milk'
26 'condensed milk'
31 curd
32 flour
39 rolls
53 sugar
65 soda",
header = TRUE, stringsAsFactors = FALSE)

关于r - 如何将单词分配给数据框中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277949/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com