Search code examples
rdplyrweighted

How to obtain the weight of a specific value in a data frame?


I have a data frame with employee responses of different companies. The responses are categorized as Yes=1 No=2. I want to know the percentage of YES (1) per company.

Company <- (a,a,b,b,b,c,c)
Response <- (1,1,2,2,1,1,2)

this is the result that I want

"yes" answers

company a   100%
company b   33%
company c   50%

Solution

  • you can achieve this with the dplyr package. Just keep in mind: in R, strings are between quotes.

    You need to put these two vectors together in a table. (created with data.frame()

    library(dplyr)
    company <- c("a", "a", "b", "b", "b", "c","c")
    Response <- c(1,1,2,2,1,1,2)
    data.frame(
      company,
      Response
    ) %>% 
      dplyr::group_by(company) %>% 
      dplyr::summarise(
        yes = mean(Response == 1)
      )
    #> # A tibble: 3 x 2
    #>   company   yes
    #>   <chr>   <dbl>
    #> 1 a       1    
    #> 2 b       0.333
    #> 3 c       0.5
    

    Created on 2022-06-17 by the reprex package (v2.0.1)