具有从R中的其他数据帧映射的值的字符串
我想根据字典中实际值所在的其他列的ID生成一个字符串 理想情况下,这看起来像:具有从R中的其他数据帧映射的值的字符串,r,tidyverse,R,Tidyverse,我想根据字典中实际值所在的其他列的ID生成一个字符串 理想情况下,这看起来像: 库(tidyverse) 区域\u dict区域颜色我的\u字符串 #> #>1注册号1列注册号1 xxx注册号1列注册号1列 #>2注册号2 col\u id2 xxx\u注册号2 col\u 2 #>3注册号3列注册号3列xxx注册号3列注册号3列 由(v0.3.0)于2021-03-01创建 我知道dplyr的recode()函数,但我想不出一种方法来使用它 我还考虑过首先使用lef
库(tidyverse)
区域\u dict区域颜色我的\u字符串
#>
#>1注册号1列注册号1 xxx注册号1列注册号1列
#>2注册号2 col\u id2 xxx\u注册号2 col\u 2
#>3注册号3列注册号3列xxx注册号3列注册号3列
由(v0.3.0)于2021-03-01创建
我知道dplyr的recode()
函数,但我想不出一种方法来使用它
我还考虑过首先使用left\u join()
,然后将新列中的字符串连接起来。这是可行的,但对我来说不太好,因为我会得到我以后需要删除的列。在真实数据集中,我有5个变量
我很乐意阅读您的想法。这也可以通过模糊连接来解决,但基于子字符串中的相似性,从每个数据的“id”列中删除前缀子字符串并执行
左连接
,然后通过将列粘贴在一起来创建“我的字符串”
library(stringr)
library(dplyr)
region_dict %>%
mutate(id1 = str_remove(id, '.*_')) %>%
left_join(color_dict %>%
mutate(id1 = str_remove(id, '.*_')), by = 'id1') %>%
transmute(region = id.x, color = id.y,
my_string = str_c('xxx_', name.x, '_', name.y))
-输出
# A tibble: 3 x 3
# region color my_string
# <chr> <chr> <chr>
#1 reg_id1 col_id1 xxx_reg_1_col_1
#2 reg_id2 col_id2 xxx_reg_2_col_2
#3 reg_id3 col_id3 xxx_reg_3_col_3
#一个tible:3 x 3
#区域颜色my_字符串
#
#1注册号1列注册号1 xxx注册号1列注册号1列
#2注册号2 col\u id2 xxx\u注册号2 col\u 2
#3注册号3列注册号3列xxx注册号3列注册号3列
谢谢,@akrun。你的回答很鼓舞人心。我不知道transmute()
存在,它引导我找到。将参数保留在mutate()
中。我已经得到了我所需要的tibble(region=c(“reg\u id1”、“reg\u id2”、“reg\u id3”)、color=c(“col\u id1”、“col\u id2”、“col\u id3”)%%>%left\u join(region\u dict%%>%rename\u with(~str\u c(“region\uu”,.x))、by=c(“region”=“region”=“region”=“region\u id”)))%%>%left\u join(color\u dict%>%rename\u用(~str\u c(“color”)、.x”)、by=c(“color”)、.x”)、%(my_string=str_c(“xxx_”,region_name,”,color_name),.keep=“unused”)