Open woneuy01 opened 4 years ago
The main types of string processing tasks are detecting, locating, extracting and replacing elements of strings. The stringr package from the tidyverse includes a variety of string processing functions that begin with str_ and take the string as the first argument, which makes them compatible with the pipe.
murders_raw$population[1:3] as.numeric(murders_raw$population[1:3])
library(tidyverse) # includes stringr
Key points Use the str_detect() function to determine whether a string contains a certain pattern. Use the str_replace_all() function to replace all instances of one pattern with another pattern. To remove a pattern, replace with the empty string (""). The parse_number() function removes punctuation from strings and converts them to numeric. mutate_at() performs the same transformation on the specified column numbers. Code
commas <- function(x) any(str_detect(x, ",")) murders_raw %>% summarize_all(funs(commas))
test_1 <- str_replace_all(murders_raw$population, ",", "") test_1 <- as.numeric(test_1)
test_2 <- parse_number(murders_raw$population) identical(test_1, test_2)
murders_new <- murders_raw %>% mutate_at(2:3, parse_number) murders_new %>% head
head(dat)
A tibble: 5 x 3
Month Sales Profit
January $128,568 $16,234 February $109,523 $12,876 March $115,468 $17,920 April $122,274 $15,825 May $117,921 $15,437
Which of the following commands could convert the sales and profits columns to numeric? Select all that apply
dat %>% mutate_at(2:3, parse_number)
dat %>% mutate_at(2:3, funs(str_replace_all(., c("\$|,"), ""))) %>% mutate_at(2:3, as.numeric)
Key points Define a string by surrounding text with either single quotes or double quotes. To include a single quote inside a string, use double quotes on the outside. To include a double quote inside a string, use single quotes on the outside. The cat() function displays a string as it is represented inside R. To include a double quote inside of a string surrounded by double quotes, use the backslash () to escape the double quote. Escape a single quote to include it inside of a string defined by single quotes. We will see additional uses of the escape later. Code s <- "Hello!" ###double quotes define a string s <- 'Hello!' ### single quotes define a string s <- Hello ### backquotes do not
s <- "10"" ### error - unclosed quotes s <- '10"' ### correct
cat shows what the string actually looks like inside R cat(s)
s <- "5'" cat(s)
to include both single and double quotes in string, escape with \ s <- '5'10"' ### error s <- "5'10"" ### error s <- '5'10"' ### correct cat(s) s <- "5'10"" ### correct cat(s)