【发布时间】:2020-05-20 16:10:41
【问题描述】:
我想使用 tidyverse 对多个列执行计算。我知道如何为单个用户执行此操作(在单个列中表示),但我需要为 1000 多个用户执行此操作(因此列数相等)。
但是,我不太熟悉使用 tidyverse 和使用 tibbles 进行计算,但我在这个平台上得到了一些早期帮助(确切的编码与下面的不同,但我把它归结为核心问题)。
数据集包含一年中的所有时间(8760 个值,365 天,每 24 小时)以及多个用户的值。
每个用户,我需要汇总特定时间范围(例如 00:00 到 03:00 之间的所有内容)之间的 正 值,然后从 03:00 到 05 之间的汇总值中减去这些值: 00(无论这些值是正值还是负值)。总共有1000多个用户。
library(tidyverse)
library(lubridate)
set.seed(4)
time_index <- seq(
from = as.POSIXct("2016-01-01 00:00"),
to = as.POSIXct("2016-12-31 23:00"),
by = "hour"
)
user1 <- runif(length(time_index), min = -1, max = 1)
user2 <- runif(length(time_index), min = -1, max = 1)
user3 <- runif(length(time_index), min = -1, max = 1)
example <- data.frame(time_index, user1, user2, user3)
单个列(用户)的代码是:
df_intermediate <- example %>%
mutate(
date = as_date(time_index),
hour = hour(time_index),
hour_block = case_when(
between(hour, 0, 2) ~ "block_1",
between(hour, 3, 5) ~ "block_2",
TRUE ~ NA_character_
)
) %>%
filter(!is.na(hour_block)) %>%
group_by(date, hour_block) %>%
nest() %>%
ungroup() %>%
mutate(
intermediate_result = if_else(
hour_block == "block_1",
map_dbl(data, ~ sum(.$user[.$user> 0 ])),
map_dbl(data, ~ sum(.$user))
)
) %>%
group_by(date) %>%
summarise(
final_result = first(intermediate_result) - last(intermediate_result)
)
这为单个用户提供以下结果:
df_intermediate
#> # A tibble: 366 x 2
#> date final_result
#> <date> <dbl>
#> 1 2016-01-01 0.469
#> 2 2016-01-02 0.189
#> 3 2016-01-03 -1.32
我无法将其扩展到多个用户。我查看了使用 mutate_at 或编写自己的函数以包含在 mutate_at 中,但我不知道如何包含条件(“first_block”中应该只有正值)和众多列。那么,如何才能对多列而不是单列进行变异呢?
【问题讨论】: