【问题标题】:R mutate to calculate relative frequency of occurrence within groupsR mutate 计算组内发生的相对频率
【发布时间】:2019-07-02 03:04:21
【问题描述】:

我需要一些帮助。

假设我有这个:

# A tibble: 10 x 3
   a         b c    
   <chr> <dbl> <lgl>
 1 a         1 TRUE 
 2 a         1 TRUE 
 3 a         1 TRUE 
 4 a         2 TRUE 
 5 a         2 TRUE 
 6 a         2 FALSE
 7 a         2 FALSE
 8 a         3 FALSE
 9 a         3 FALSE
10 a         3 FALSE

structure(list(a = c("a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a"), b = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3), c = c(TRUE, TRUE, 
TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

我想group_byb 并在每个组内计算T == TRUE 在列c 中的相对频率以生成列d

所以我想要这个输出:

# A tibble: 10 x 4
   a         b c         d
   <chr> <dbl> <lgl> <dbl>
 1 a         1 TRUE    1  
 2 a         1 TRUE    1  
 3 a         1 TRUE    1  
 4 a         2 TRUE    0.5
 5 a         2 TRUE    0.5
 6 a         2 FALSE   0.5
 7 a         2 FALSE   0.5
 8 a         3 FALSE   0  
 9 a         3 FALSE   0  
10 a         3 FALSE   0  

structure(list(a = c("a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a"), b = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3), c = c(TRUE, TRUE, 
TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE), d = c(1, 
1, 1, 0.5, 0.5, 0.5, 0.5, 0, 0, 0)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

最好使用dplyrtidyverse

我试过了:

#1
t %>% 
  group_by(b) %>%
  mutate(
    d = nrow(c[c == T])/nrow()
  )
#2
t %>% 
  group_by(b) %>%
  mutate(
    d = count(c[c == T])/count()
  )
#3 
t %>% 
  group_by(b) %>%
  mutate(
    d = nrow(any(c[c == T]))/nrow(any())
  )

两者都不起作用。

类似的问题(但不同):
How to calculate the relative frequency per groups
R: relative frequency in r by factor

任何帮助表示赞赏。
提前致谢。

【问题讨论】:

  • 在我看来,这个问题是那些“类似问题”(可能还有其他问题)的副本。不知道为什么重新打开。
  • @neifws OP在帖子中已经提到这不是他的观点Similar questions (but different):的欺骗@
  • @neilfws 我打开它是因为我认为它是一个新问题或更具体的问题,因为我无法使用任何其他问题来解决我的问题。

标签: r tidyverse


【解决方案1】:

通常是为了找出变量在我们做的组中出现的次数

df %>%
  group_by(b) %>%
  mutate(d = sum(c == TRUE)/n())

但是由于这里c 是逻辑向量,我们也可以取c 中的sum,然后除以组中的行数。

library(dplyr)

df %>%
  group_by(b) %>%
  mutate(d = sum(c)/n())

#   a         b c         d
#   <chr> <dbl> <lgl> <dbl>
# 1 a         1 TRUE    1  
# 2 a         1 TRUE    1  
# 3 a         1 TRUE    1  
# 4 a         2 TRUE    0.5
# 5 a         2 TRUE    0.5
# 6 a         2 FALSE   0.5
# 7 a         2 FALSE   0.5
# 8 a         3 FALSE   0  
# 9 a         3 FALSE   0  
#10 a         3 FALSE   0  

【讨论】:

    【解决方案2】:

    我们可以在按'b'分组后取'c'的mean

    library(dplyr)
    df1 %>%
        group_by(b) %>% 
        mutate(d = mean(c))
    # A tibble: 10 x 4
    # Groups:   b [3]
    #   a         b c         d
    #   <chr> <dbl> <lgl> <dbl>
    # 1 a         1 TRUE    1  
    # 2 a         1 TRUE    1  
    # 3 a         1 TRUE    1  
    # 4 a         2 TRUE    0.5
    # 5 a         2 TRUE    0.5
    # 6 a         2 FALSE   0.5
    # 7 a         2 FALSE   0.5
    # 8 a         3 FALSE   0  
    # 9 a         3 FALSE   0  
    #10 a         3 FALSE   0  
    

    注意:mean - 定义 - 您习惯的“平均值”,将所有数字相加,然后除以数字的数量。


    另一种选择是

    df1 %>%
       group_by(b) %>%
       mutate(d = sum(as.integer(c))/n())
    

    或使用data.table

    library(data.table)
    setDT(df1)[, d := mean(c), by = b]
    

    或使用base R

    df1$d <- with(df1, ave(c, b))
    

    【讨论】:

    • 这么简单,非常感谢。也许,这是一个非常快速的答案。我需要等待 6 分钟才能投票。 :D
    • 如果我想在一个字符列中计数!= NA_character_
    • @AurelianoGuedes Cha 是什么。如果是字符串,则df1 %&gt;%group_by(b) %&gt;% mutate(e = sum(!is.na(col)))
    • @AurelianoGuedes 对于NA,无论是NA_real_NA_integer_ 还是NA_character_is.na 适用于所有这些返回 TRUE/FALSE 逻辑向量并从那里,可以得到向量的sum,其中TRUE被编码为1和FALSE 0
    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2023-04-05
    • 2018-10-25
    • 2022-01-27
    • 2022-08-18
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多