使用str_detect函数在R dataframe中有条件地创建一个新列?

使用str_detect函数在R dataframe中有条件地创建一个新列?,r,R,我有一个数据框,其中列a包含值: **Channel** Direct Paid social Organic social 我想做的是:创建一个名为groupedChannel的新列,其中str_detect搜索列a中的字符串,以在groupedChannel中添加值 Condition: IF row in Column A matches regex "direct" THEN Column B value = "Direct" ELSE IF row in Column B match

我有一个数据框,其中列a包含值:

**Channel**
Direct
Paid social
Organic social
我想做的是:创建一个名为groupedChannel的新列,其中str_detect搜索列a中的字符串,以在groupedChannel中添加值

Condition:
IF row in Column A matches regex "direct" THEN Column B value = "Direct" ELSE
IF row in Column B matches regex "social" THEN Column B value = "Social"

抱歉,str_detect将仅返回TRUE/FALSE。如何使用TRUE/FALSE在B列中赋值?

我有一个
数据表
基于条件替换的解决方案。它使用
grepl
,但如果需要,可以使用
stringr::str\u detect

库(data.table)
setDT(df)
df[,groupedChannel:=“社交”]
#条件替换
df[grepl(“direct”,colA),groupedChannel:=“direct”]

(解决方案未经测试)

我有一个
数据表
基于条件替换的解决方案。它使用
grepl
,但如果需要,可以使用
stringr::str\u detect

库(data.table)
setDT(df)
df[,groupedChannel:=“社交”]
#条件替换
df[grepl(“direct”,colA),groupedChannel:=“direct”]

(解决方案未经测试)

使用基本R正则表达式函数的解决方案,在通道列中未找到direct和social时也会处理

# Dummy data
data <- data.frame(Channel = c("Direct Paid", "Social", "Organic", "Social Organic"),
                   stringsAsFactors = F)

# Use sapply to iterate through each value in the 'Channel' column in the above dataframe
data$groupChannel <- sapply(data$Channel, FUN = function(x){
  # Use base R regex functions to for conditions, and return values for new column
  if (grepl("direct", tolower(x))){
    return("Direct")
  }else if (grepl("social", tolower(x))){
    return("Social")
  }else{
    return("Direct or Social Not Found")
  }
})

head(data)
  Channel               groupChannel
1    Direct Paid                     Direct
2         Social                     Social
3        Organic Direct or Social Not Found
4 Social Organic                     Social
#虚拟数据

数据使用基本R正则表达式函数的解决方案,在通道列中找不到direct和social时也会处理

# Dummy data
data <- data.frame(Channel = c("Direct Paid", "Social", "Organic", "Social Organic"),
                   stringsAsFactors = F)

# Use sapply to iterate through each value in the 'Channel' column in the above dataframe
data$groupChannel <- sapply(data$Channel, FUN = function(x){
  # Use base R regex functions to for conditions, and return values for new column
  if (grepl("direct", tolower(x))){
    return("Direct")
  }else if (grepl("social", tolower(x))){
    return("Social")
  }else{
    return("Direct or Social Not Found")
  }
})

head(data)
  Channel               groupChannel
1    Direct Paid                     Direct
2         Social                     Social
3        Organic Direct or Social Not Found
4 Social Organic                     Social
#虚拟数据

数据您想要的是匹配您的正则表达式,而不是简单地检测

库(dplyr)
图书馆(stringr)
蒂布尔(
colA=c(“频道**”、“直接”、“付费社交”、“有机社交”)
) %>% 
变异(
colB=str_match(colA,“[Ss]social |[Dd]direct”)[,1],
colB=从上到下(colB)
)
#>#tibble:4 x 2
#>可乐瓶
#>              
#>1**频道**
#>2直接
#>3.有偿社会福利
#>4有机社会
由(v0.3.0)于2020年4月29日创建

stringr::str_match
返回一个矩阵,其中第一列是匹配本身,多个组的后续列,因此我们需要将
[,1]
放在该调用的末尾。然后它匹配大小写版本,所以我们将所有匹配的组转换为小写


或者,您可以像这样使用
str_extract
colB=str_extract(colA,[Ss]social |[Dd]direct”),
而不使用
[,1]

您想要的是匹配您的正则表达式,而不是简单地检测

库(dplyr)
图书馆(stringr)
蒂布尔(
colA=c(“频道**”、“直接”、“付费社交”、“有机社交”)
) %>% 
变异(
colB=str_match(colA,“[Ss]social |[Dd]direct”)[,1],
colB=从上到下(colB)
)
#>#tibble:4 x 2
#>可乐瓶
#>              
#>1**频道**
#>2直接
#>3.有偿社会福利
#>4有机社会
由(v0.3.0)于2020年4月29日创建

stringr::str_match
返回一个矩阵,其中第一列是匹配本身,多个组的后续列,因此我们需要将
[,1]
放在该调用的末尾。然后它匹配大小写版本,所以我们将所有匹配的组转换为小写


或者,您可以像这样使用
str_extract
colB=str_extract(colA,[Ss]ocial |[Dd]direct”),
而不使用
[,1]

这里有一个
基本R
解决方案,它假设您有一组明确定义的
通道组

数据:

现在,使用
sub
Channel
值替换为
Channel\u组的值
\\U
确保这些值作为大写字符串返回(如果希望使用小写字符串,请使用
\\L
):


这里有一个
base R
解决方案,它假设您有一组明确定义的
Channel\u group

数据:

现在,使用
sub
Channel
值替换为
Channel\u组的值
\\U
确保这些值作为大写字符串返回(如果希望使用小写字符串,请使用
\\L
):


嗨,杰米。谢谢,成功了。是否有一个dplyr等价于base R的grep1函数?嗨,杰米。谢谢,成功了。是否有一个dplyr等价于base R中的grep1函数?
a <- c("(S|s)ocial", "(D|d)irect")
data$Channel_group <- sub(paste0(".*\\b(", paste(a, collapse = "|"),")\\b.*"), "\\U\\1", data$Channel, perl = T)
data
         Channel Channel_group
1         Direct        DIRECT
2    Paid social        SOCIAL
3 Organic social        SOCIAL