【问题标题】:R - Look for a string on multiple conditions and replace it in a new columR - 在多个条件下查找字符串并将其替换为新列
【发布时间】:2015-01-25 07:04:44
【问题描述】:

我有一个数据框,其中主要列是:"source""Medium"。我需要根据"source""Medium" 的值在名为"Campanas1" 的新列上分配"value"

问题是:

使用我的代码(见下文)时,我的 col "contentGroupUniqueViews1" 的值与原始数据框不匹配。而且代码有点混乱(不紧凑)。

有没有办法:

在 data.frame 中工作,并应用一系列 ifelse 条件以在新列中输出结果? 我不想子集化然后重新加入,因为它存在重复行的高风险。

有没有更简单的方法在新列中进行 ifelse 搜索和替换? 或者至少这样做,所以我的价值观与原始价值观相匹配?我认为问题出在推荐的正则表达式中,但我现在没有看到。

这些是我的步骤:

  1. 多个条件的子集(结合这 2 列)。
  2. 使用我需要的新值创建一个新列。例如If "souce" == "google" and "Medium" == organic" then "Búsquedas"
  3. 我将这些子集放入新变量中:DirectoBúsquedasAdwordsSocial_MediaEmailReferenciasRRSS
  4. 然后使用rbind 连接所有子集以重新创建原始子集。

这是我的代码:

Directo <- subset(x = Total_Content, grepl("\\(direct\\)", source))
Directo$Campanas1 <- "Directo"

Búsquedas <- subset(x = Total_Content, grepl("google", source) & grepl("organic", Medium))
Búsquedas$Campanas1 <- "Búsquedas"

Adwords <- subset(Total_Content, grepl("google", source) & grepl("cpc", Medium) | grepl("cpv",      Medium))
Adwords$Campanas1 <- "Adwords"

Social_Media <- subset(Total_Content, grepl(".*faceb.*", source) | grepl("social\\-media",   Medium))
Social_Media$Campanas1 <- "Social Media"

Email <- subset(Total_Content, grepl(".*mail.*", source) | grepl(".*mail.*", Medium))
Email$Campanas1 <- "Email"

RRSS <- subset(Total_Content, grepl(".*terra.*", source) | grepl("elcomercio", source)|   grepl("diario16", source)| grepl(".*laprensa.*", source))
RRSS$Campanas1 <- "RRSS"

Referencias <- subset(Total_Content, grepl(".*esp.*", source) & !grepl(".*faceb.*", source) &     !grepl(".*mail.*", source) &  !grepl(".*comercio.*", source) &  !grepl(".*prensa.*", source) &     !grepl(".*diario.*", source) &  !grepl(".*terra.*", source) & grepl("referral", Medium))
Referencias$Campanas1 <- "Referencias"

Total <- rbind(Directo,Búsquedas,Adwords, Social_Media, Email, Referencias, RRSS)`

【问题讨论】:

    标签: regex r search replace


    【解决方案1】:

    与其子集,修改,然后rbind重新组合,我认为只修改原始数据更干净。

    我不会尝试做所有事情,因为没有要测试的数据,所以我可能会打错字,原则应该概括。像这样的:

    Total_Content$Campanas1 <- NA # initialize this column in case it is not there yet
    
    Total_Content$Campanas1[grepl("\\direct\\", Total_Content$source)] <- "Directo"
    Total_Content$Campanas1[grepl("google", Total_Content$source)
                            & grepl("organic", Total_Content$medium)] <- "Búsquedas"
    

    等等。如果您不喜欢多次输入Total_Content,可以使用with()。您可以对ifelse 使用相同的技术:

    Total <- Total_Content
    Total$Campanas1 <- with(Total, ifelse(grepl("\\direct\\", source), "Directo",
                           ifelse(grepl("google", source)
                                  & grepl("organic", medium), "Búsquedas",
                               ifelse(...))))
    

    您也可以使用dplyr 来节省一些输入。它看起来像这样(您也可以将ifelse 嵌套在dplyr 中):

    library(dplyr)
    Total <- mutate(Total_Content,
        Campanas1 = NA,
        Campanas1 = ifelse(grepl("\\direct\\", source), "Directo", Campanas1),
        Campanas1 = ifelse(grepl("google", source)
                           & grepl("organic", medium), "Búsquedas", Campanas1))
    

    等等。

    【讨论】:

    • 谢谢@Gregor,我会试试这个。但另一个重要的问题是:如何简化来自 Referencencias 的这个正则表达式?以及如何确保它不会与其他正则表达式混合(制造麻烦)?是否有一个包可以执行此搜索和替换并将结果“标记”在不同的列中?
    • 我不确定你的意思。也许您可以使用 10 或 15 行示例输入数据以及您想要的输出。
    • 使用我的方法,Campanas1 被初始化为NA。您可以将Referencias 的结果放在另一列Campanas2 中,并查看它选择了哪些行——看看它是否只有NA
    • 如果Referencias 应该是所有其他东西之后剩下的所有东西,那么嵌套的ifelse() 语句也可以工作。没有示例输入/输出很难知道。
    • 我最担心的是,当使用来自 dplyr 的 summarize 时,原始 data.frame 和新 data.frame 的总数不匹配。我将通过一些输入来编辑问题。谢谢。
    猜你喜欢
    • 2020-08-21
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2014-02-11
    相关资源
    最近更新 更多