如何在数据框架中获得特定值的重量?

发布于 2025-02-08 11:39:12 字数 254 浏览 1 评论 0 原文

我有一个数据框架,其中包括不同公司的员工回应。响应分为Yes = 1否= 2。我想知道每家公司是(1)的百分比。

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

这是我想要的结果

"yes" answers

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

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%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素食主义者 2025-02-15 11:39:12

在基础中,您可以做:

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

什至:

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

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
最偏执的依靠 2025-02-15 11:39:12

您可以使用DPLYR包装来实现此目标。
只需记住:在R中,字符串在引号之间。

您需要将这两个向量放在桌子中。 (使用 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

在2022-06-17创建的(v2.0.1)

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)

So要识趣 2025-02-15 11:39:12

尝试一下

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 % 

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 % 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文