I want to divide multiple columns of a dataframe by a column of that same dataframe (to get percentages). I would like to do it with purrr, to be able to iterate the operation for each column, and I tried to do it in the following way, but failed.
#create the tibble
competitor_a <- c(125,300,250)
competitor_b <- c(321,452,366)
competitor_c <- c(231,271,139)
data <- tibble(competitor_a,competitor_b,competitor_c)
data <- data %>%
mutate(total_sales=sum(competitor_a,competitor_b,competitor_c))
#iteration
data %>%
map(select(competitor_a,competitor_b,competitor_c)/total_sales))
where data
is my dataframe, competitor_a, competitor_b, competitor_c
are columns with the sales of the competitors (dividend), and total_sales
are my divider.
In this case I think it is better to use across
:
library(dplyr)
data <- data %>% mutate(across(competitor_a:competitor_c, `/`, total_sales))
This can also be done using base R:
data[1:3] <- data[1:3]/data$total_sales
Your total_sales
column is incorrect. Because you're summing across rows, you need to do the following (using dplyr
):
data <- data %>%
rowwise() %>%
mutate(total_sales = sum(c_across(everything())))
As for the map, one way is to use an anonymous function:
data %>%
map_dfr(~.x/data$total_sales*100) %>%
select(-total_sales)
# A tibble: 3 x 3
competitor_a competitor_b competitor_c
<dbl> <dbl> <dbl>
1 9.23 23.7 17.1
2 14.7 22.1 13.2
3 16.6 24.2 9.21
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.