gpt4 book ai didi

r - 计算分组数据框中的滚动标准差

转载 作者:行者123 更新时间:2023-12-03 23:59:22 25 4
gpt4 key购买 nike

我需要一种好方法来遍历大型数据框并对组进行计算。

这是我的示例数据:

df_names <- data.frame()
for (int_x in 1: 1000)
{
df_names <- rbind( df_names, sample (c("John", "Peter", "Michael", "Lisa", "George", "Linda",
"Fresco", "Pope", "Niclas", "Rammen"),1))
}


df <- data.frame(date= Sys.Date() + sort(sample(1:5000, 1000)),
score= runif(1000, min = 25, max=500),
names= df_names)

现在我想找到每个名称的滚动标准偏差(按日期排序)我这样做;

unique_names <- unique(df$names)
for (int_y in 1:NROW(unique_names)){

df %>% filter(names== unique_names[int_y]) %>% arrange(date) %>% select( score)%>% as.matrix() %>%
rollapplyr( 5, sd, fill = 0) }

Trouble1:如何让矩阵回到原始数据帧?麻烦2:这是一种不好的做法,我认为..有没有一种整洁的做法

最佳答案

您正在计算滚动的 sd 但很快将其丢弃,我们可以修复它。另外,我认为我们可以改进流程以充分利用 tidyverse 分组,并将所有内容放在一起。

set.seed(42)
df_names <- data.frame()
for (int_x in 1: 1000) {
df_names <- rbind( df_names, name = sample (c("John", "Peter", "Michael", "Lisa", "George", "Linda",
"Fresco", "Pope", "Niclas", "Rammen"),1))
}
df <- data.frame(date = Sys.Date() + sort(sample(1:5000, 1000)),
score = runif(1000, min = 25, max=500),
name = df_names[[1]])
str(df)
# 'data.frame': 1000 obs. of 3 variables:
# $ date : Date, format: "2020-10-02" "2020-10-03" "2020-10-08" "2020-10-15" ...
# $ score: num 285.1 343 41.7 78.7 351.7 ...
# $ name : chr "John" "George" "John" "Niclas" ...

流程:

library(dplyr)
# library(zoo) # rollapply
as_tibble(df) %>%
arrange(date) %>%
group_by(name) %>%
mutate(rollsd = zoo::rollapply(score, 5, sd, fill = 0)) %>%
ungroup() %>%
slice(100:120) # arbitrary, just to show the middle
# # A tibble: 21 x 4
# date score name rollsd
# <date> <dbl> <chr> <dbl>
# 1 2022-02-22 365. Niclas 74.7
# 2 2022-02-26 388. George 107.
# 3 2022-02-27 275. Niclas 74.7
# 4 2022-03-05 171. Rammen 158.
# 5 2022-03-07 500. Pope 150.
# 6 2022-03-08 278. Fresco 81.2
# 7 2022-03-11 37.7 Linda 204.
# 8 2022-03-12 46.9 John 180.
# 9 2022-03-13 224. George 110.
# 10 2022-03-16 302. Niclas 63.4
# # ... with 11 more rows

关于r - 计算分组数据框中的滚动标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64145367/

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