3 ms·
While I agree that it easily can be abused, I quickly got the hang of it and found it makes a lot of my code more readable. One could think of it as a "then".
by thestephen 9y ago
While I agree that it easily can be abused, I quickly got the hang of it and found it makes a lot of my code more readable. One could think of it as a "then".
my_column %>%
gsub(" ","",.) %>% # Remove whitespace, then...
gsub("[a-zA-Z]+","",.) %>% # Remove letters, then...
strsplit("-") %>% # Split on dashes, then...
lapply(as.numeric) %>% # Make each vector in the list numeric, then...
lapply(mean) # calculate the mean for each list index
The alternatives here would be gigantic function calls that need to be read from the inside out, or multiple variable assignments.
- in9 9y agoIdk why, but looking at the traditional base-R functions really hurt my eyes. The inconsistent api (like in gsub, where the string comes last) together with the unclear naming (very Unixy when you think about it) made it very hard for me when getting starting with R. Now days I would do the same with the tidyverse equivalents: my_column %>% stringr::str_replace_all(" ","") %>% stringr::str_replace_all("[a-zA-Z]+","") %>% stringr::str_split("-") %>% purrr::map(is.numeric) %>% purrr::map(mean)
- thestephen 9y agoVery nice idea to use the tidyverse equivalents – thanks!
- in9 9y agoSomething that I do now and then, that really screws up the readability, is when I want to join a dataframe with another, but forget to manipulate the second. And it turns to something like: df %>% select(var_1, var_2, var_3) %>% left_join(df2 %>% select(another_var1, var2), by = c('var_2', 'var2')) This is really over using `%>%`. However, I sometimes take this pattern way too far... :D
- ap53 9y agoI do the same thing all the time... How would you improve it? Thanks!