I want to divide each column in dataframe which satisfy a condition by one of the columns within the same dataframe
I believe it can be done in several ways, but I feel that it would be more efficient for me if I could do it using purrr package which is designed for this type of work.
for example, this works
t <- iris %>%
modify_if(is.numeric, ~./2)
but this does not
t <- iris %>%
modify_if(is.numeric, ~./Sepal.Length)
this results in error
Error in .f(.x[[i]], ...) : object 'Sepal.Length' not found
I think it wants me to specify .y as "Sepal.Length" but I could not figure out the right way.
I am confused between modify2, imodify, map2 and pmap
I would appreciate if you can point me to a proper guide on how to use purrr properly. The official guide seems to be okay for the concept and intuition
but I am struggling to put it in application.
https://purrr.tidyverse.org/reference/modify.html
iris %>% mutate_if(is.numeric, ~ ./Sepal.Length)
. – r2evansSepal.Length
column is correct. This is because it's the first column, so it divides by itself to give 1 and then all other columns are divided by 1 i.e. unchanged. – neilfwsmodify_if
and thoughtmutate_if
, ... – r2evansmutate_if
if the columns were reordered to placeSepal.Length
last. – neilfws