Given a grouped data.frame
and a list
containing total numbers referring to another characteristic for each group (70 for group 1, 90 for group 2):
group <- c(1,1,1,1,2,2,2,2,2)
n<- c(2,4,10,2,4,5,2,8,9)
df <- data.frame(group, n) %>%
group_by(group)
mylist <- list(70, 90)
How can I add a new column to the data.frame
that reflects the proportion of each n
in mylist
for the respective group given by n/mylist[[i]]*100
?
I thought about using map_dbl
to iterate over the list elements, however, I can't get my head around how to call these commands in mutate
(something like df %>% mutate ("Percent" = n / map_dbl (mylist, .)*100)
) doing the percent calculation to finally make it look like this:
df$percent %>% c (2.9, 5.7, 14.3, 2.9, 4.4, 5.6, 2.2., 8.9, 10.0)
df
What would be an elegant way to call the list
elements to include them into the calculation?
Perhaps this
df %>% mutate(p = n/map_dbl(group, ~mylist[[.]]) * 100)
Basically, mapping group to pull out the selected element of mylist.
You might also consider using a join.
I know it doesn't use purrr
, but how about just rowwise()
?
library(dplyr)
df %>%
rowwise %>%
mutate(percent = n / mylist[[group]] * 100)
## A tibble: 9 x 3
# group n percent
# <dbl> <dbl> <dbl>
#1 1 2 2.86
#2 1 4 5.71
#3 1 10 14.3
#4 1 2 2.86
#5 2 4 4.44
#6 2 5 5.56
#7 2 2 2.22
#8 2 8 8.89
#9 2 9 10
You can represent your list data as data.frame first to make it easier to work with.
library(dplyr)
library(data.table)
group <- c(1,1,1,1,2,2,2,2,2)
n<- c(2,4,10,2,4,5,2,8,9)
df <- data.frame(group, n) %>%
group_by(group)
setDT(df)
mylist <- data.table(
group = c(1 ,2),
other.metric = c(70, 90)
)
dt <- merge(df, mylist, by = "group")
dt[, n_share := n / other.metric * 100]
dt
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.