Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- name <- c('john','carl', 'hank')
- salary <- c('$23,456.33','$45,677.43','$76,234.88')
- emp_data <- data.frame(name,salary)
- clean <- function(ttt){
- as.numeric(gsub('[^a-zA-z0-9.]','', ttt))
- }
- sapply(emp_data, clean)
- library(dplyr)
- library(stringr)
- emp_data %>%
- mutate_if(~any(str_detect(., '^\$')),
- ~as.numeric(str_replace_all(., '[$,]', '')))
- doll_cols <- sapply(emp_data, function(x) any(startsWith(as.character(x), '$')))
- emp_data[doll_cols] <- lapply(emp_data[doll_cols],
- function(x) as.numeric(gsub('\$|,', '', x)))
Add Comment
Please, Sign In to add comment