使用str_replace()函数来检测带有’+’字符串的字符串

huangapple go评论90阅读模式
英文:

Use str_replace() to detect a string with a '+' string

问题

我想清洗sales_method列,使'email'变为'Email','em + call'变为'Email + Call'。但是当我使用str_replace_all()时,它只能检测和替换'email'。是否有一种方法可以替换其中包含'+'符号的字符串?

我尝试了以下代码:

  1. rep_str = c('email'='Email', 'em + call'='Email + Call')
  2. sales_no_na$sales_method <- str_replace_all(sales_no_na$sales_method, rep_str)

然后结果如下图所示:

英文:

使用str_replace()函数来检测带有’+’字符串的字符串

I wanted to clean the sales_method column such that 'email' would go under 'Email' and 'em + call' would go under 'Email + Call'. But when I use str_replace_all(), it was only able to detect and replace 'email'. Is there a way to replace the string with the '+' sign in it?

I tried

  1. rep_str = c(&#39;email&#39;=&#39;Email&#39;, &#39;em + call&#39;=&#39;Email + Call&#39;)
  2. sales_no_na$sales_method &lt;- str_replace_all(sales_no_na$sales_method, rep_str)

which then ended up like this

使用str_replace()函数来检测带有’+’字符串的字符串

答案1

得分: 2

你必须使用 \\ 转义加号。使用以下代码:

  1. rep_str = c('email'=>'Email', 'em \\+ call'=>'Email + Call')
  2. df$sales_method <- str_replace_all(df$sales_method, rep_str)
  3. > df
  4. # A tibble: 5 × 2
  5. count sales_method
  6. <dbl> <chr>
  7. 1 6915 Email
  8. 2 4781 Call
  9. 3 2203 Email + Call
  10. 4 20 Email + Call
  11. 5 7 Email
英文:

You have to escape the plus sign using \\. Use

  1. rep_str = c(&#39;email&#39;=&#39;Email&#39;, &#39;em \\+ call&#39;=&#39;Email + Call&#39;)
  2. df$sales_method &lt;- str_replace_all(df$sales_method, rep_str)
  3. &gt; df
  4. # A tibble: 5 &#215; 2
  5. count sales_method
  6. &lt;dbl&gt; &lt;chr&gt;
  7. 1 6915 Email
  8. 2 4781 Call
  9. 3 2203 Email + Call
  10. 4 20 Email + Call
  11. 5 7 Email

答案2

得分: 0

  1. 请尝试以下代码
  2. ```r
  3. df %>% mutate(sales_method=str_replace_all(sales_method, c('call'='Call','email'='Email','em'='Email'))) %>%
  4. group_by(sales_method) %>%
  5. summarise(count=sum(count)) %>%
  6. ungroup()
  1. # 一个 tibble: 3 × 2
  2. 销售方法 计数
  3. <chr> <dbl>
  4. 1 电话 4781
  5. 2 电子邮件 6922
  6. 3 电子邮件 + 电话 2223
  1. <details>
  2. <summary>英文:</summary>
  3. Please try the below code
  4. ```r
  5. df %&gt;% mutate(sales_method=str_replace_all(sales_method, c(&#39;call&#39;=&#39;Call&#39;,&#39;email&#39;=&#39;Email&#39;,&#39;em&#39;=&#39;Email&#39;))) %&gt;%
  6. group_by(sales_method) %&gt;%
  7. summarise(count=sum(count)) %&gt;% ungroup()
  1. # A tibble: 3 &#215; 2
  2. sales_method count
  3. &lt;chr&gt; &lt;dbl&gt;
  4. 1 Call 4781
  5. 2 Email 6922
  6. 3 Email + Call 2223

huangapple
  • 本文由 发表于 2023年7月14日 05:26:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定