How to mean a variable with condition in another variable (statement)

Hi @anatolia
You can do this.

library(dplyr, warn.conflicts = FALSE)
library(tidyr)

df <- tibble::tibble(University1 = c(1,0,0,1), University2 = c(1,1,0,0), University3 = c(1,1,0,1) ,Productivity = c(12,13,5,2) )


df
#> # A tibble: 4 x 4
#>   University1 University2 University3 Productivity
#>         <dbl>       <dbl>       <dbl>        <dbl>
#> 1           1           1           1           12
#> 2           0           1           1           13
#> 3           0           0           0            5
#> 4           1           0           1            2

df_gather <- tidyr::gather(data = df, University , value, -Productivity )


res <- 
  df_gather %>% 
  filter(value == 1) %>% 
  group_by(University) %>% 
  summarise(prod_univ = mean(Productivity, na.rm = T))


res
#> # A tibble: 3 x 2
#>   University  prod_univ
#>   <chr>           <dbl>
#> 1 University1       7  
#> 2 University2      12.5
#> 3 University3       9

Created on 2019-11-27 by the reprex package (v0.2.1)

For better help post a reprex example insead of an image of your dataframe:

1 Like