我试图有条件地将信息从一个变量粘贴到另一个变量中,这样我就从拥有像dfsummary这样的数据框架变成了像dfgoal这样的数据框架。
在尝试中,我尝试使用^和$来使grepl()精确匹配,但是它不起作用。另外,我的尝试仅处理dfsummary $ SumVarName [1],而我希望它能在所有dfsummary $ SumVarName中起作用。
感谢您的帮助!
# Starting point
dfsummary <- data.frame(SumVarName=c("CarMake, SK4+","CarMake, SK4","Customer, highend - in","Customer, highend - out","price, expensive"),SumVarNumber=c("12 (43%)","16 (57%)","9 (39%)","11 (61%)","8 (40%)"))
dfmain <- data.frame(MainName=c("CarMake, SK4+ vs ref","Customer, highend - in vs ref"))
# Goal
dfgoal<- data.frame(MainName=c("CarMake, SK4+ vs ref","Customer, highend - in vs ref"),MainVarNumber=c("12 (43%)","9 (39%)"))
# Attempt
dfmain$MainVarNumber <- NA
dfmain$MainVarNumber[1][grepl(^dfsummary$SumVarName[1]$,dfmain$MainName[1])] <- paste0(dfsummary$SumVarNumber[1])
答案 0 :(得分:1)
一个简单的想法是将列分隔为vs
,合并然后再unite
再将两列,即(在tidyverse
中)
library(tidyverse)
dfmain %>%
separate(MainName, into = c('SumVarName', 'v2'), sep = ' vs ') %>%
left_join(dfsummary) %>%
unite(MainName, SumVarName, v2, sep = ' vs ')
给出,
#Joining, by = "SumVarName" MainName SumVarNumber 1 CarMake vs SK4+_ref 12 (43%) 2 Customer vs highend - in_ref 9 (39%)
答案 1 :(得分:0)
与Sotos类似的想法...
library(dplyr)
merge(dfmain,
mutate(dfsummary,
SumVarName = paste0(dfsummary$SumVarName, ' vs ref')),
by.y = 'SumVarName', by.x = 'MainName')