Home > front end >  How to obtain the weight of a specific value in a data frame?
How to obtain the weight of a specific value in a data frame?

Time:06-18

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%

CodePudding user response:

In base R you can do:

prop.table(table(company, Response),1) * 100
       Response
company         1         2
      a 100.00000   0.00000
      b  33.33333  66.66667
      c  50.00000  50.00000

or even:

mosaic::tally(~Response|company, data.frame(company, Response), format='percent')
        company
Response         a         b         c
       1 100.00000  33.33333  50.00000
       2   0.00000  66.66667  50.00000

CodePudding user response:

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)

CodePudding user response:

Try this

df %>% group_by(Data_Company) %>% 
summarise(yes = paste(round(mean(Response == 1) * 100) , "%") )


# A tibble: 3 × 2
  Data_Company yes  
  <chr>        <chr>
1 a            100 %
2 b            33 % 
3 c            50 % 
  • Related