I have a dataset with many columns with similar names. Some of the columns have values in cents and others in dollars, e.g:
library (tidyverse)
data<- tribble(
~col1_cents, ~col1,~col2_cents, ~col2,
1000, NA, 3000, NA,
NA, 20, NA, 25.2,
2000, NA, 2030, NA,
)
For one variable, it's easy to divide the value by 100 and then assign it to the dollar variable, and delete the cent variable e.g.:
data %>% mutate( if_else(is.na(col1),
col1_cents/100,
col1) %>%
select(-col1_cents)
Is there a generalisable way to do this for all variables in the dataset that end in _cents? I tried this with mutate_at and ends_with but could not get it to rename to the original variable without _cents...
Thanks!