无法grepl并将值替换为字符串中的“ +”

时间:2019-08-13 00:22:52

标签: r

我无法替换表中的某些值。我不确定grepl函数为何适用于某些单元格,但不适用于某些单元格。

我有下表:

table = structure(list(V1 = c("Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report"), V2 = c("Deanna", "Deanna", "Deanna", 
"Phil", "Phil", "Phil", "Phil", "Mark", "Mark", "Mark", 
"Mark", "Stacey+Phil", "Stacey+Phil", "Stacey+Phil", "Stacey+Phil", 
"Total", "Total", "Total", "Total"), RiskType = c("Sales", 
"Sales", "Sales", "Sales", "Sales", "Sales", "Sales", 
"Sales", "Sales", "Sales", "Sales", "Sales", "Sales", 
"Sales", "Sales", "Sales", "Sales", "Sales", "Sales"
), CurrencyResults = c("AUD", "NZD", "Total", "AUD", "NZD", "Total", "USD", 
"AUD", "NZD", "Total", "USD", "AUD", "NZD", "Total", "USD", "AUD", 
"NZD", "Total", "USD"), Negative = c(-51620.213555, 771045.326333, 
-771144.54622, 10556202.43205, 055.2236103, 117135173.415, 
267535.3406626, -1002727.533523, 654110.620213, -74206.1167267, 
2601630.71527, 5271375.30625, 154465.21202, 1163307.207, 
5277016.067541, -71514.716726, 2315741.242135, 3201242.344134, 
5277016.067541), Positive = c(123220741.41277, -75014.37554353, 
123145726.575232, 325145.10762, -122044.746563, 133166.11266, 
-644.240654, 150726.322746, -604036.510037, 1474561.332542, 
-77272127.5011, 131732571.403, -110021.56656, 235652.051540, 
-4267021.367, 25453312.026, -114036.43722, 151502254.626773, 
-4267021.367), MaxLoss = c(-51620.213555, -75014.37554353, 
-771144.54622, 325145.10762, -122044.746563, 133166.11266, 
-644.240654, -1002727.533523, -604036.510037, -74206.1167267, 
-77272127.5011, 5271375.30625, -110021.56656, 235652.051540, 
-4267021.367, -71514.716726, -114036.43722, 3201242.344134, 
-4267021.367)), class = "data.frame", row.names = c(NA, -19L
))

如果我使用

table %>% mutate(V2 = replace(V2, str_detect(V2, "^Mark$"), "Terry"))

它运作良好(并用特里代替了马克斯。

但是如果我尝试同样的事情,

table %>% mutate(V2 = replace(V2, str_detect(V2, "^Stacey+Phil$"), "Terry"))

它不起作用。

我已经做了一些搜索,我认为它可能与正则表达式有关,但是我没有找到解决方法。我正在寻找一种仅用短语“ Terry”代替“ Stacey + Phil”的方法。任何帮助将不胜感激。

0 个答案:

没有答案