Library
library(stringr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
str_c : String combine
statement<-c("R", "is powerful", "tool", "for data", "analysis")
str_c(c("My Name", NA, "Jhon"),".")
## [1] "My Name." NA "Jhon."
str_c : Join strings
# Join strings (equivalent to base R paste())
str_c("Join", "Me!", sep="_")
## [1] "Join_Me!"
# Join strings (equivalent to base R paste())
str_c(c("Join", "vectors"), c("Me!", "too!"), sep="_")
## [1] "Join_Me!" "vectors_too!"
str_c : Collapse a vector of strings into a single string
str_c(c("Turn", "me", "into", "one", "string!"), collapse= " ")
## [1] "Turn me into one string!"
str_replace_na : NA Replace
str_replace_na(c("My Name", NA, "Jhon"),".")
## [1] "My Name" "." "Jhon"
str_subset : pattern : Matching pattern
str_subset(colors(),pattern="green")
## [1] "darkgreen" "darkolivegreen" "darkolivegreen1"
## [4] "darkolivegreen2" "darkolivegreen3" "darkolivegreen4"
## [7] "darkseagreen" "darkseagreen1" "darkseagreen2"
## [10] "darkseagreen3" "darkseagreen4" "forestgreen"
## [13] "green" "green1" "green2"
## [16] "green3" "green4" "greenyellow"
## [19] "lawngreen" "lightgreen" "lightseagreen"
## [22] "limegreen" "mediumseagreen" "mediumspringgreen"
## [25] "palegreen" "palegreen1" "palegreen2"
## [28] "palegreen3" "palegreen4" "seagreen"
## [31] "seagreen1" "seagreen2" "seagreen3"
## [34] "seagreen4" "springgreen" "springgreen1"
## [37] "springgreen2" "springgreen3" "springgreen4"
## [40] "yellowgreen"
str_subset : pattern : Matching pattern (start(^) with ‘orange’ or
end($) with ‘red’)
str_subset(colors(),pattern="^orange|red$")
## [1] "darkred" "indianred" "mediumvioletred" "orange"
## [5] "orange1" "orange2" "orange3" "orange4"
## [9] "orangered" "orangered1" "orangered2" "orangered3"
## [13] "orangered4" "palevioletred" "red" "violetred"
substring : the characters from 1,11
df<-("Journal_dev_private_limited")
substring(df,1,11)
## [1] "Journal_dev"
substring : returns the characters from 1-7
df<-("Journal_dev")
substring(df,1,7)
## [1] "Journal"
substring : returns the string by replacing the _ by space
df<-("We are_developers")
substring(df,7,7)=" "
df
## [1] "We are developers"
substring : string replacement
df<-("R=is a language made for statistical analysis")
substring(df,2,2)=" "
df
## [1] "R is a language made for statistical analysis"
substring : replaces the 4th letter of each string by $
df<-c("Alok","Joseph","Hayato","Kelly","Paloma","Moca")
substring(df,4,4)<-c("$")
df
## [1] "Alo$" "Jos$ph" "Hay$to" "Kel$y" "Pal$ma" "Moc$"
substr or str_sub : creates the data frame
df<-data.frame(Technologies=c("Datascience","machinelearning","Deeplearning","Artificalintelligence"),Popularity=c("70%","85%","90%","95%"))
#creates new column with extracted values
df$Extracted_Technologies1=substr(df$Technologies,10,15)
df$Extracted_Technologies2=str_sub(df$Technologies,10,15)
df
## Technologies Popularity Extracted_Technologies1
## 1 Datascience 70% ce
## 2 machinelearning 85% arning
## 3 Deeplearning 90% ing
## 4 Artificalintelligence 95% intell
## Extracted_Technologies2
## 1 ce
## 2 arning
## 3 ing
## 4 intell
str_view : html view
str_view(statement,"a.")
str_detect : detect value (TRUE / FALSE)
x <- c("apple", "banana", "pear")
str_detect(x, "e")
## [1] TRUE FALSE TRUE
str_detect : sum of detected words start with t
# How many common words start with t?
sum(str_detect(words, "^t"))
## [1] 65
str_detect : mean of the common words end(S) with a vowel
mean(str_detect(words, "[aeiou]$"))
## [1] 0.2765306
str_to_sentence : Convert string to sentence (only first letter of
first word uppercase)
str_to_sentence("make me into a SENTENCE!")
## [1] "Make me into a sentence!"
str_trim : Trim whitespace
str_trim(" Trim Me! ")
## [1] "Trim Me!"
str_pad : Pad strings with whitespace
str_pad("Pad Me!", width = 15, side="both")
## [1] " Pad Me! "
str_trunc : Truncate strings to a given length
str_trunc("If you have a long string, you might want to truncate it!",
width = 50)
## [1] "If you have a long string, you might want to tr..."
str_split : Split strings
str_split("Split Me!", pattern = " ")
## [[1]]
## [1] "Split" "Me!"
str_sort : sort string alphabeticall
sort_data <- c("sort", "me", "please!")
str_sort(sort_data)
## [1] "me" "please!" "sort"
str_order: order value alphabeticall
str_order(sort_data)
## [1] 2 3 1
str_glue : String Interpolation
first <- c("Luke", "Han", "Jean-Luc")
last <- c("Skywalker", "Solo", "Picard")
str_glue("My name is {first}. {first} {last}.")
## My name is Luke. Luke Skywalker.
## My name is Han. Han Solo.
## My name is Jean-Luc. Jean-Luc Picard.
str_glue : Interpolate the result of an execution into a string
minimum_age <- 18
over_minimum <- c(5, 17, 33)
str_glue("{first} {last} is {minimum_age + over_minimum} years old.")
## Luke Skywalker is 23 years old.
## Han Solo is 35 years old.
## Jean-Luc Picard is 51 years old.
str_glue : Interpolate the result of function calls
num <- c(1:5)
str_glue("The square root of {num} is {round(sqrt(num), 3)}.")
## The square root of 1 is 1.
## The square root of 2 is 1.414.
## The square root of 3 is 1.732.
## The square root of 4 is 2.
## The square root of 5 is 2.236.
str_glue_data : Interpolate strings using data from a data
frame
library(tibble)
mtcars %>% rownames_to_column("Model") %>%
filter(mpg > 30) %>%
str_glue_data("The {Model} gets {mpg} mpg.")
## The Fiat 128 gets 32.4 mpg.
## The Honda Civic gets 30.4 mpg.
## The Toyota Corolla gets 33.9 mpg.
## The Lotus Europa gets 30.4 mpg.