Chapter 6 Working with Strings
6.1 Remove a pattern from a string
=tribble(~car, ~price,
price_table"Corvette", "$65,000",
"Mustang GT", "$40,000")
# BASE R METHOD (sub by replacing something with nothing)
gsub("\\$", "",price_table$price) # (pattern, replace with, object$column)
## [1] "65,000" "40,000"
# TIDYVERSE METHOD
str_remove(price_table$price, pattern = "\\$")
## [1] "65,000" "40,000"
You can remove numbers by typing "[:digit:]"
$cgi_sev=str_remove(panss_sem_data$cgi_sev, pattern = "[:digit:]") panss_sem_data
6.2 Replace one pattern in a string with another
Tidyverse command: str_replace()
or str_replace_all()
Base R command: gsub()
# base R
gsub(mtcars, replacement = )
#tidyverse
str_replace_all(iris$Species, pattern=c("e", "a"), replacement="ZZZZ") |>
head()
str_replace(iris$Species, pattern=c("e", "a"), replacement="ZZZZ") |>
head()
6.3 Find (i.e., filter for) all instances of a string
Useful for finding very specific things inside a column (e.g., one particular person’s name in a roster of names; everyone with a particular last name)
Tidyverse command: str_detect()
Base R command: grepl()
Note both must be nested inside of filter()
=rownames_to_column(mtcars, var = "car")
cars_df
# base R
|> filter(grepl("Firebird", car))
cars_df
# tidyverse
%>% filter(str_detect(car,"Firebird")) cars_df
You can also search for multiple strings simultaneously by including the “or” logical operator inside the quotes.
|> filter(str_detect(car, "Firebird|Fiat")) cars_df
You can also include the negation logical operator to filter for all instances except those with the specified string.
# base R
|> filter(!(grepl("Pontiac", car)))
cars_df
# tidyverse
|> filter(!(str_detect(car, "Pontiac"))) cars_df