平均每个点

发布于 2025-01-23 21:13:14 字数 976 浏览 0 评论 0原文

DPUT数据

df <- structure(list(Log_Score = c(1.02036009176836, 1.04731899428056, 
1.26147129638715, 2.01321383095142, 1.21566884408913, 1.04241773032447
), Time_Stamp = 1:6, Time_Log_Mean = c(0, 0, 0, 0, 0, 0)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
> df
# A tibble: 6 x 3
  Log_Score Time_Stamp Time_Log_Mean
      <dbl>      <int>         <dbl>
1      1.02          1             0
2      1.05          2             0
3      1.26          3             0
4      2.01          4             0
5      1.22          5             0
6      1.04          6             0

我正在尝试使用for for循环创建每个时间点的平均值,但似乎无法使其正常工作。我使用的代码是:

for (i in 1:nrow(df)) {
    df$Time_Log_Mean[i] <- ((sum(df$Log_Score[1:i]))/(df$Time_Stamp[i]))
}

该代码在初次给我错误消息后神秘地开始工作:

分配的数据值必须与现有数据兼容。 x存在 数据有335行。 X分配的数据有6553行。我只有 尺寸1被回收。

Dput data

df <- structure(list(Log_Score = c(1.02036009176836, 1.04731899428056, 
1.26147129638715, 2.01321383095142, 1.21566884408913, 1.04241773032447
), Time_Stamp = 1:6, Time_Log_Mean = c(0, 0, 0, 0, 0, 0)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
> df
# A tibble: 6 x 3
  Log_Score Time_Stamp Time_Log_Mean
      <dbl>      <int>         <dbl>
1      1.02          1             0
2      1.05          2             0
3      1.26          3             0
4      2.01          4             0
5      1.22          5             0
6      1.04          6             0

I am trying to create a mean average for every point in time using a for loop but can't seem to get it to work. The code I was using is:

for (i in 1:nrow(df)) {
    df$Time_Log_Mean[i] <- ((sum(df$Log_Score[1:i]))/(df$Time_Stamp[i]))
}

The code has mysteriously started to work after initial giving me the error message:

Assigned data value must be compatible with existing data. x Existing
data has 335 rows. x Assigned data has 6553 rows. i Only vectors of
size 1 are recycled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2025-01-30 21:13:14

看起来像是

df <-
  structure(
    list(
      Log_Score = c(
        1.02036009176836,
        1.04731899428056,
        1.26147129638715,
        2.01321383095142,
        1.21566884408913,
        1.04241773032447
      ),
      Time_Stamp = 1:6,
      Time_Log_Mean = c(0, 0, 0, 0, 0, 0)
    ),
    row.names = c(NA,-6L),
    class = c("tbl_df", "tbl", "data.frame")
  )

library(tidyverse)
df %>% 
  mutate(res = cummean(Log_Score))
#> # A tibble: 6 × 4
#>   Log_Score Time_Stamp Time_Log_Mean   res
#>       <dbl>      <int>         <dbl> <dbl>
#> 1      1.02          1             0  1.02
#> 2      1.05          2             0  1.03
#> 3      1.26          3             0  1.11
#> 4      2.01          4             0  1.34
#> 5      1.22          5             0  1.31
#> 6      1.04          6             0  1.27

reprex package (v2.0.1.1(v2.0.1.1(v2.0.1), )

looks like a cumulative average calculation

df <-
  structure(
    list(
      Log_Score = c(
        1.02036009176836,
        1.04731899428056,
        1.26147129638715,
        2.01321383095142,
        1.21566884408913,
        1.04241773032447
      ),
      Time_Stamp = 1:6,
      Time_Log_Mean = c(0, 0, 0, 0, 0, 0)
    ),
    row.names = c(NA,-6L),
    class = c("tbl_df", "tbl", "data.frame")
  )

library(tidyverse)
df %>% 
  mutate(res = cummean(Log_Score))
#> # A tibble: 6 × 4
#>   Log_Score Time_Stamp Time_Log_Mean   res
#>       <dbl>      <int>         <dbl> <dbl>
#> 1      1.02          1             0  1.02
#> 2      1.05          2             0  1.03
#> 3      1.26          3             0  1.11
#> 4      2.01          4             0  1.34
#> 5      1.22          5             0  1.31
#> 6      1.04          6             0  1.27

Created on 2022-04-25 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文