Guest User

Untitled

a guest
Jul 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. name <- c('john','carl', 'hank')
  2. salary <- c('$23,456.33','$45,677.43','$76,234.88')
  3. emp_data <- data.frame(name,salary)
  4.  
  5. clean <- function(ttt){
  6. as.numeric(gsub('[^a-zA-z0-9.]','', ttt))
  7. }
  8. sapply(emp_data, clean)
  9.  
  10. library(dplyr)
  11. library(stringr)
  12.  
  13. emp_data %>%
  14. mutate_if(~any(str_detect(., '^\$')),
  15. ~as.numeric(str_replace_all(., '[$,]', '')))
  16.  
  17. doll_cols <- sapply(emp_data, function(x) any(startsWith(as.character(x), '$')))
  18. emp_data[doll_cols] <- lapply(emp_data[doll_cols],
  19. function(x) as.numeric(gsub('\$|,', '', x)))
Add Comment
Please, Sign In to add comment