Reputation: 876
I need to replace a whole sentence with a string. For example:
# I want to replace "coarse particulate matter (Hourly measured)" with "pm10"
species <- c("coarse particulate matter (Hourly measured)","nitrogen dixoide")
# I tried:
sub("coarse particulate matter (Hourly measured)","pm10",species)
gsub("coarse particulate matter (Hourly measured)","pm10",species)
str_replace(species,"coarse particulate matter (Hourly measured)","pm10")
But none of them works. Many thanks for help.
In the real data, the orders are random. That is why I can not use things like
species[1] <- "pm10"
Upvotes: 1
Views: 117
Reputation: 1364
If you want to use str_replace
you may need fixed
str_replace(species, fixed("coarse particulate matter (Hourly measured)", ignore_case = TRUE), "pm10")
#[1] "pm10" "nitrogen dixoide"
Upvotes: 1
Reputation: 31
species[species == "coarse particulate matter (Hourly measured)"] <- "pm10"
Upvotes: 1
Reputation: 101335
You can try with option fixed = TRUE
> sub("coarse particulate matter (Hourly measured)","pm10",species,fixed = TRUE)
[1] "pm10" "nitrogen dixoide"
> gsub("coarse particulate matter (Hourly measured)","pm10",species,fixed = TRUE)
[1] "pm10" "nitrogen dixoide"
Upvotes: 1
Reputation: 887108
Here, we don't need sub
species[1] <- "pm10"
or use ==
species[species == "coarse particulate matter (Hourly measured)"] <- "pm10"
Upvotes: 1