gpt4 book ai didi

r - 带 dplyr 的条件 rowMeans

转载 作者:行者123 更新时间:2023-12-05 02:25:58 26 4
gpt4 key购买 nike

我想使用 dplyr 将条件 rowMeans 应用于每组 ID。可重现的表格如下:

dat <- as.Date("2021/08/04")
len <- 4
seq(dat, by = "day", length.out = len)

input <- data.frame(
date = c(seq(dat, by = "day", length.out = len) , seq(dat, by = "day", length.out = len)),
user_id = c(rep("aa",4),rep("bb",4)),
var1 = c(1:4),
var2 = c(4,7,23,9,0,0,0,0),
var3 = c(0,0,0,0,4,8,2,7)
)

逻辑是:对于每个 ID,首先选择数字列且它们的总和为 =!0,然后计算选定列的 rowMean那个ID。

所需的输出表如下:

 output = data.frame(
date = c(seq(dat, by = "day", length.out = len) , seq(dat, by = "day", length.out = len)),
user_id = c(rep("aa",4),rep("bb",4)),
var1 = c(1:4),
var2 = c(4,7,23,9,0,0,0,0),
var3 = c(0,0,0,0,4,8,2,7),
rowAverage = as.numeric(c(rowMeans(input[1:4,3:4]), rowMeans(input[5:8,c(3,5)])))
)

我想到的行如下,但我遇到了一些错误:

output = input %>%
dplyr::group_by(user_id) %>%
dplyr::mutate(rowAverage = rowMeans(select_if(function(x) {(is.numeric(x)) && (sum(x)=!0)})))

你能帮我改正这个吗?谢谢。

最佳答案

你很亲近。您也只需将数据提供给 select_if()。为此使用 cur_data()

input %>%
group_by(user_id) %>%
mutate(rowAverage = rowMeans(
select_if(cur_data(), \(x) is.numeric(x) && sum(x) != 0)
))
#> # A tibble: 8 × 6
#> # Groups: user_id [2]
#> date user_id var1 var2 var3 rowAverage
#> <date> <chr> <int> <dbl> <dbl> <dbl>
#> 1 2021-08-04 aa 1 4 0 2.5
#> 2 2021-08-05 aa 2 7 0 4.5
#> 3 2021-08-06 aa 3 23 0 13
#> 4 2021-08-07 aa 4 9 0 6.5
#> 5 2021-08-04 bb 1 0 4 2.5
#> 6 2021-08-05 bb 2 0 8 5
#> 7 2021-08-06 bb 3 0 2 2.5
#> 8 2021-08-07 bb 4 0 7 5.5

关于r - 带 dplyr 的条件 rowMeans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74152431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com