sum multiple variables by group type in one df without subsetting

Refresh

December 2018

Views

46 time

1

I'm looking for a quicker way to summarize by group type, for many different groups in one df without having to subset. Below is a sample data frame and the current code I use to accomplish it. It seems verbose to me, and I'm guessing there is a quicker way to resolve this. In this example, my code summarizes health revenue grouped by name, then merges it back into the master. I'd like to summarize both health and vision variables, grouped by name. The key, is that I only want revenues for health and vision, when there is a 1 in the variable. Thanks for your help.

#df
name = c("jerry","jerry","jerry","dave","dave","dave","mary","mary","mary") 
health = c(1,0,1,1,0,1,0,1,1) 
vision = c(0,1,0,0,1,0,1,0,0) 
rev =c(100,200,500,1000,800,300,400,600,300)
df = data.frame(name,health,vision,rev) 


#Subset health
health = subset(df, health == 1) 


#Sum by group type
library(dplyr)
health <- health %>% group_by(name) %>% 
  mutate(
    health_rev=sum(rev, na.rm = TRUE))


#Select variables
health <- health[c("name","health_rev")]


#Remove duplicates
health <- health[!duplicated(health$name), ]


#Merge back to master
master <- merge(x = df, y = health, by = "name", all.x = TRUE)

1 answers

3

Что-то вроде этого?

df %>% 
  group_by(name) %>% 
  mutate(health_rev = sum(rev[as.logical(health)]), 
          vision_rev = sum(rev[as.logical(vision)])) %>% 
  ungroup()

Результат:

# A tibble: 9 × 6
   name health_rev vision_rev health vision   rev
  <chr>      <dbl>      <dbl>  <dbl>  <dbl> <dbl>
1  dave       1300        800      1      0  1000
2  dave       1300        800      0      1   800
3  dave       1300        800      1      0   300
4 jerry        600        200      1      0   100
5 jerry        600        200      0      1   200
6 jerry        600        200      1      0   500
7  mary        900        400      0      1   400
8  mary        900        400      1      0   600
9  mary        900        400      1      0   300
Смотреть фильмы онлайн бабушка легкого поведения 2 фильм бесплатно