R语言 计算每组相同特定值的百分比

egdjgwm8  于 2023-04-03  发布在  其他
关注(0)|答案(3)|浏览(118)

我有以下 Dataframe df(dput如下):

> df
   group class value
1      A FALSE     2
2      A  TRUE     1
3      A FALSE     1
4      A FALSE     2
5      A FALSE     3
6      B FALSE     4
7      B FALSE     2
8      B  TRUE     2
9      B FALSE     2
10     B FALSE     6
11     C  TRUE     5
12     C FALSE     5
13     C FALSE     3
14     C FALSE     5
15     C FALSE     5

我想计算每组特定相似值的百分比。总有一个值带有class == TRUE,这意味着我想计算与class == TRUE值相似的值的百分比。正如你在上面的数据框中看到的,A组的值为1,class == TRUE,A组中有两个值为1,所以2/5 = 0。其中4个值为1。下面是所需的输出:

group value pct
1     A     1 0.4
2     B     2 0.6
3     C     5 0.8

所以我想知道是否有人知道如何计算R中每组特定值的百分比?
df的dput

df <- structure(list(group = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "B", "C", "C", "C", "C", "C"), class = c(FALSE, TRUE, FALSE, 
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, 
FALSE, FALSE, FALSE), value = c(2, 1, 1, 2, 3, 4, 2, 2, 2, 6, 
5, 5, 3, 5, 5)), class = "data.frame", row.names = c(NA, -15L
))
mbjcgjjk

mbjcgjjk1#

试试看

library(dplyr)#version >= 1.10
df %>%
   reframe(pct = mean(value == value[class]), value = value[class], .by = group)
  • 输出
group pct value
1     A 0.4     1
2     B 0.6     2
3     C 0.8     5

或使用data.table选项

library(data.table)
setDT(df)[df[(class)], .(value = i.value,
  pct = mean(value == i.value)), on = .(group), by = .EACHI]
   group value pct
1:     A     1 0.4
2:     B     2 0.6
3:     C     5 0.8
70gysomp

70gysomp2#

您可以:

library(dplyr)
df %>%
  group_by(group) %>%
  summarize(pct = sum(value == value[class == TRUE])/n(),
            value = value[class == TRUE])

# A tibble: 3 x 3
  group   pct value
  <chr> <dbl> <dbl>
1 A       0.4     1
2 B       0.6     2
3 C       0.8     5
j5fpnvbx

j5fpnvbx3#

带有ave + subset的基本R选项

subset(
  transform(
    df,
    pct = ave(ave(class, group, value) > 0, group)
  ), 
  class
)

给予

group class value pct
2      A  TRUE     1 0.4
8      B  TRUE     2 0.6
11     C  TRUE     5 0.8

相关问题