gpt4 book ai didi

r - R data.frame 中多个变量的每小时平均值?

转载 作者:行者123 更新时间:2023-12-04 02:32:25 24 4
gpt4 key购买 nike

我有以下代码,并试图找到每个 变量(即 X、Y 和 Z)小时均值。我的输出应该是一个包含 hourlyDate 列和所有 variablesmean hourly datadata.frame。任何前进的方式将不胜感激。

library(lubridate)

set.seed(123)

T <- data.frame(Datetime = seq(ymd_hms("2011-01-01 00:00:00"), to= ymd_hms("2011-12-31 00:00:00"), by = "5 min"),
X = runif(104833, 5,10),Y = runif(104833, 5,10), Z = runif(104833, 5,10))
T$Date <- format(T$Datetime, format="%Y-%m-%d")
T$Hour <- format(T$Datetime, format = "%H")
T$Mints <- format(T$Datetime, format = "%M")

最佳答案

尝试:

library(lubridate)
library(dplyr)

set.seed(123)

T <- data.frame(Datetime = seq(ymd_hms("2011-01-01 00:00:00"), to= ymd_hms("2011-12-31 00:00:00"), by = "5 min"),
X = runif(104833, 5,10),Y = runif(104833, 5,10), Z = runif(104833, 5,10))



T %>% mutate(hourlyDate = floor_date(Datetime,unit='hour')) %>%
select(-Datetime) %>% group_by(hourlyDate) %>%
summarize(across(everything(),mean)) %>%
ungroup()
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 8,737 x 4
#> hourlyDate X Y Z
#> <dttm> <dbl> <dbl> <dbl>
#> 1 2011-01-01 00:00:00 8.00 7.90 6.90
#> 2 2011-01-01 01:00:00 7.93 7.47 7.90
#> 3 2011-01-01 02:00:00 7.83 6.89 7.67
#> 4 2011-01-01 03:00:00 6.61 7.92 7.18
#> 5 2011-01-01 04:00:00 7.27 7.20 6.48
#> 6 2011-01-01 05:00:00 7.88 6.80 7.69
#> 7 2011-01-01 06:00:00 7.07 8.05 7.52
#> 8 2011-01-01 07:00:00 7.40 7.92 6.99
#> 9 2011-01-01 08:00:00 7.97 7.76 7.26
#> 10 2011-01-01 09:00:00 7.57 7.47 6.94
#> # ... with 8,727 more rows

reprex package 创建于 2020-08-20 (v0.3.0)

关于r - R data.frame 中多个变量的每小时平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63509390/

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