![](/img/trans.png)
[英]Is there a way to access the entire tibble/grouped tibble from the `mutate()` function acting on it?
[英]sum a col from a grouped tibble
我有以下數據框:
library(tidyverse)
datasetmax <- data.frame(pearl = c('new','new','recurrent','recurrent','new'),
jam = c(10,20,30,40,50),
dog = c(1,2,3,1,2),
stringsAsFactors = FALSE)
我將數據框分組為一個摘要:
datasetmax %>%
mutate(temple = ifelse(pearl == 'new', jam * 0.1, jam * 0.2)) %>%
group_by(pearl) %>% summarise(plus = sum(temple), weight = sum(jam*dog)/sum(dog))
我想要列權重(兩個值)的總和,應該是 62.5
我嘗試了以下代碼:
datasetmax %>%
mutate(temple = ifelse(pearl == 'new', jam * 0.1, jam * 0.2)) %>%
group_by(pearl) %>% summarise(plus = sum(temple), weight = sum(jam*dog)/sum(dog)) %>% colSums(plus)
但它告訴我“加號”列不存在。 為什么? 你能幫我一把嗎? 謝謝你。
像這樣:
datasetmax %>%
mutate(temple = ifelse(pearl == 'new', jam * 0.1, jam * 0.2)) %>%
group_by(pearl) %>% summarise(plus = sum(temple), weight = sum(jam*dog)/sum(dog)) %>%
select(weight) %>% sum()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.