R将数据帧中的字符串匹配替换为来自另一个数据帧/数组的值

问题描述 投票:-1回答:2

我创建了一个代码来匹配来自不同的字符串值,如果匹配则将字符串值替换为更晚的字符串值

我有一个数据框,另一个是数组,在第一个

df1 <- data.frame(campaign_source=c("googleadwords", "google display" ,"twitter banner", "facebook-post", "facebook like","inmobi","organic"),cost=c(4,2,3,4,5,6,7))

source<-c("google","facebook","twitter")

目标是在df1中创建一个新列,该列应该具有基于任何与源df1 $ campaign_source匹配的文本的值,所以我使用了

df1$n_campaign_source<-"other"   

    for (k in 1:nrow(df1))
{
  for(i in 1:length(source)){
    h<-df1[k,]$campaign_source
    h1<-df1[k,]$n_campaign_source
    j <- grep(source[i],h )
    if(is.na(j[1]) == FALSE & h1 !='other'){
      df1[k,]$n_campaign_source<-source[i]
    }
  }}

这个需要花费很多时间,任何更快的解决方案都是最终输出

no campaign_source cost n_campaign_source
1  googleadwords    4            google
2  google display    2            google
3  twitter banner    3          facebook
4   facebook-post    4          facebook
5   facebook like    5           twitter
6          inmobi    6             other
7         organic    7             other
r grep
2个回答
1
投票

(上面的答案似乎不正确。)尝试使用grep结果作为赋值索引的替代代码:

 df1$source <- NA
 for( item in source ) df1$source[grep(item,  df1$campaign_source)] <- item
 df1$source[is.na(df1$source)] <- "other"
 df1
#-----------------
  campaign_source cost   source
1  google adwords    4   google
2  google display    2   google
3  twitter banner    3  twitter
4   facebook post    4 facebook
5   facebook like    5 facebook
6          inmobi    6    other
7         organic    7    other

0
投票

以下是使用strsplit的替代解决方案:

df1$source <- sapply(df1$campaign_source, function(x) {
    w <- unlist(strsplit(as.character(x), " "));
    if (length(w[w %in% source]) > 0) w[w %in% source] else "other";
})
#campaign_source cost   source
#1  google adwords    4   google
#2  google display    2   google
#3  twitter banner    3  twitter
#4   facebook post    4 facebook
#5   facebook like    5 facebook
#6          inmobi    6    other
#7         organic    7    other

样本数据

df1 <- data.frame(campaign_source=c("google adwords", "google display" ,"twitter banner", "facebook post", "facebook like","inmobi","organic"),cost=c(4,2,3,4,5,6,7))

source<-c("google", "facebook", "twitter");
© www.soinside.com 2019 - 2024. All rights reserved.