简体   繁体   English

如何调用列表元素来计算新的 data.frame 列

[英]How to call list elements for calculation of a new data.frame column

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):给定一个分组的data.frame和一个包含引用每个组的另一个特征的总数的list (第 1 组为 70,第 2 组为 90):

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 ?如何向data.frame添加一个新列,以反映n/mylist[[i]]*100给出的各个组的mylist中每个n的比例?

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:我考虑过使用map_dbl来遍历列表元素,但是,我无法理解如何在mutate中调用这些命令(类似于df %>% mutate ("Percent" = n / map_dbl (mylist, .)*100) ) 进行百分比计算,最终使它看起来像这样:

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?调用list元素以将它们包含在计算中的优雅方法是什么?

Perhaps this也许这

df %>% mutate(p = n/map_dbl(group, ~mylist[[.]]) * 100)

Basically, mapping group to pull out the selected element of mylist.基本上,映射组以拉出 mylist 的选定元素。

You might also consider using a join.您也可以考虑使用联接。

I know it doesn't use purrr , but how about just rowwise() ?我知道它不使用purrr ,但是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.您可以首先将列表数据表示为 data.frame 以使其更易于使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM