Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Input some text into a vector
- StringVector<-c("Good","Nice","Smart","Cool")
- #This function will return the position of the string we are looking for in the vector, if it exist
- grep("Cool",StringVector)
- #if it doesnot exist the function will return
- grep("BAD",StringVector)
- #grepl works in the same way as grep
- #but instead of returning the position, it retuns a logical result for each vector position
- grepl("Cool",StringVector)
- #Let's see if we can check a if column exists in a dataframe.
- #we will use the dataset mtcars
- head(mtcars)
- #for example, we want to have a list of all the cars that start with the letter "M"
- mtcars[grep("M",row.names(mtcars)),]
- #substitute a string with an other one:
- gsub("Cool","Not",StringVector)
- #for a data frame:
- d<-mtcars
- row.names(d)<-gsub("Mazda","MZ",row.names(d))
- #Let's replace white spaces with " _ "
- #here we will need the stringr library
- library(stringr)
- row.names(d)<-str_replace(row.names(d)," ","_")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement