Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #Input some text into a vector
  2. StringVector<-c("Good","Nice","Smart","Cool")
  3. #This function will return the position of the string we are looking for in the vector, if it exist
  4. grep("Cool",StringVector)
  5. #if it doesnot exist the function will return
  6. grep("BAD",StringVector)
  7.  
  8. #grepl works in the same way as grep
  9. #but instead of returning the position, it retuns a logical result for each vector position
  10. grepl("Cool",StringVector)
  11.  
  12. #Let's see if we can check a if column exists in a dataframe.
  13. #we will use the dataset mtcars
  14. head(mtcars)
  15. #for example, we want to have a list of all the cars that start with the letter "M"
  16.  
  17. mtcars[grep("M",row.names(mtcars)),]
  18.  
  19. #substitute a string with an other one:
  20. gsub("Cool","Not",StringVector)
  21. #for a data frame:
  22. d<-mtcars
  23. row.names(d)<-gsub("Mazda","MZ",row.names(d))
  24.  
  25. #Let's replace white spaces with " _ "
  26. #here we will need the stringr library
  27. library(stringr)
  28. row.names(d)<-str_replace(row.names(d)," ","_")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement