插值数据集的平均值和标准差 (R)

发布于 2025-01-16 00:20:29 字数 3542 浏览 1 评论 0原文

我有 8 个已插值的数据集,因此 x 轴间距相同,但长度不同,范围为 800-1200 点。 我想做的是计算每个 y 值的平均值和标准差。理想情况下,会输出 1200 个点,其中平均值和标准差位于不同的列中,从那里我可以绘制平均 y 值的图表并添加误差线。 我无法提供数据本身,但这就是设置。 任何正确方向的帮助或指示将不胜感激!

#s1 k = 0.4 800 data points
S1dat <- data.frame(x=S[[1]][["K"]], S[[1]][["ShearStress"]])
S1datapprox <- data.frame(approx(S1dat$x, S1dat$y, n = 800))
#s2 k = 0.6 1200 data points
S2dat <- data.frame(x=S[[2]][["K"]], S[[2]][["ShearStress"]])
S2datapprox <- data.frame(approx(S2dat$x, S2dat$y, n = 1200))
#s3 k = 0.34 680 data points
S3dat <- data.frame(x=S[[3]][["K"]], S[[3]][["ShearStress"]])
S3datapprox <- data.frame(approx(S3dat$x, S3dat$y, n = 800))
#s4 k = 0.5 1000 data points
S4dat <- data.frame(x=S[[4]][["K"]], S[[4]][["ShearStress"]])
S4datapprox <- data.frame(approx(S4dat$x, S4dat$y, n = 1000))
#s5 k = 0.4 800 data points
S5dat <- data.frame(x=S[[5]][["K"]], S[[5]][["ShearStress"]])
S5datapprox <- data.frame(approx(S5dat$x, S5dat$y, n = 800))
#s6 k = 0.36 720 data points
S6dat <- data.frame(x=S[[6]][["K"]], S[[6]][["ShearStress"]])
S6datapprox <- data.frame(approx(S6dat$x, S6dat$y, n = 720))
#s7 k = 0.4 800 data points
S7dat <- data.frame(x=S[[7]][["K"]], S[[7]][["ShearStress"]])
S7datapprox <- data.frame(approx(S7dat$x, S7dat$y, n = 800))
#s8 k = 0.54 1080 data points
S8dat <- data.frame(x=S[[8]][["K"]], S[[8]][["ShearStress"]])
S8datapprox <- data.frame(approx(S8dat$x, S8dat$y, n = 1080))
str(S)
List of 8
 $ :'data.frame':   805 obs. of  3 variables:
  ..$ K           : num [1:805] 0 0.000498 0.000996 0.001494 0.001992 ...
  ..$ ShearStress : num [1:805] 0 178 356 578 841 ...
  ..$ NormalStress: num [1:805] 0 -1.77 -5.35 -7.14 -11 -15 -16.7 -20.4 -22 -23.6 ...
 $ :'data.frame':   1500 obs. of  3 variables:
  ..$ K           : num [1:1500] 0 0.0004 0.000801 0.001201 0.001602 ...
  ..$ ShearStress : num [1:1500] 0 23.6 38.3 43.7 68.3 ...
  ..$ NormalStress: num [1:1500] 0.1 -1.34 -2.49 -4.04 -5.7 -7.28 -9.08 -10.7 -12.5 -14.3 ...
 $ :'data.frame':   812 obs. of  3 variables:
  ..$ K           : num [1:812] 0 0.000419 0.000838 0.001257 0.001676 ...
  ..$ ShearStress : num [1:812] 0 243 547 973 1280 ...
  ..$ NormalStress: num [1:812] 0 -0.89 -3.63 -6.05 -8.7 -11.5 -14.1 -16.9 -19.3 -22.1 ...
 $ :'data.frame':   853 obs. of  3 variables:
  ..$ K           : num [1:853] 0 0.000587 0.001174 0.00176 0.002347 ...
  ..$ ShearStress : num [1:853] 0 246 480 756 1246 ...
  ..$ NormalStress: num [1:853] 0 1 3 3 4 ...
 $ :'data.frame':   916 obs. of  3 variables:
  ..$ K           : num [1:916] 0 0.000437 0.000874 0.001312 0.001749 ...
  ..$ ShearStress : num [1:916] 0 44.4 67.1 89.2 119.1 ...
  ..$ NormalStress: num [1:916] 0 -0.01 -2.08 -5.06 -7.06 -9.4 -11.9 -14.6 -17.2 -20.1 ...
 $ :'data.frame':   329 obs. of  3 variables:
  ..$ K           : num [1:329] 0 0.000213 0.000536 0.001105 0.001871 ...
  ..$ ShearStress : num [1:329] 0 52.7 174.7 415.4 740.5 ...
  ..$ NormalStress: num [1:329] 0 -29.1 -30.4 -31.8 -33 -34.2 -35.3 -36.4 -38.3 -39.8 ...
 $ :'data.frame':   790 obs. of  3 variables:
  ..$ K           : num [1:790] 0 0.000237 0.000745 0.001252 0.00176 ...
  ..$ ShearStress : num [1:790] 0 94.8 347.7 633.8 1215.9 ...
  ..$ NormalStress: num [1:790] 0 -6 -12 -17 -28.4 ...
 $ :'data.frame':   1060 obs. of  3 variables:
  ..$ K           : num [1:1060] 0 0.00051 0.00102 0.00153 0.00204 ...
  ..$ ShearStress : num [1:1060] 0 44.2 70.4 100.3 133.3 ...
  ..$ NormalStress: num [1:1060] 0 0.1 0.18 -0.2 -1.2 ... ````

I have 8 data sets that I have interpolated so the x axis spacing is the same but they are different lengths ranging from 800-1200 points.
What I would like to do is then calculate the mean of each y value and the standard deviation. Ideally there would be an output of 1200 points with the mean and standard deviation in separate columns and from there I can plot a graph of the mean y value and add error bars.
I can't give the data itself but this is the setup.
Any help or pointers in the right direction would be greatly appreciated!

#s1 k = 0.4 800 data points
S1dat <- data.frame(x=S[[1]][["K"]], S[[1]][["ShearStress"]])
S1datapprox <- data.frame(approx(S1dat$x, S1dat$y, n = 800))
#s2 k = 0.6 1200 data points
S2dat <- data.frame(x=S[[2]][["K"]], S[[2]][["ShearStress"]])
S2datapprox <- data.frame(approx(S2dat$x, S2dat$y, n = 1200))
#s3 k = 0.34 680 data points
S3dat <- data.frame(x=S[[3]][["K"]], S[[3]][["ShearStress"]])
S3datapprox <- data.frame(approx(S3dat$x, S3dat$y, n = 800))
#s4 k = 0.5 1000 data points
S4dat <- data.frame(x=S[[4]][["K"]], S[[4]][["ShearStress"]])
S4datapprox <- data.frame(approx(S4dat$x, S4dat$y, n = 1000))
#s5 k = 0.4 800 data points
S5dat <- data.frame(x=S[[5]][["K"]], S[[5]][["ShearStress"]])
S5datapprox <- data.frame(approx(S5dat$x, S5dat$y, n = 800))
#s6 k = 0.36 720 data points
S6dat <- data.frame(x=S[[6]][["K"]], S[[6]][["ShearStress"]])
S6datapprox <- data.frame(approx(S6dat$x, S6dat$y, n = 720))
#s7 k = 0.4 800 data points
S7dat <- data.frame(x=S[[7]][["K"]], S[[7]][["ShearStress"]])
S7datapprox <- data.frame(approx(S7dat$x, S7dat$y, n = 800))
#s8 k = 0.54 1080 data points
S8dat <- data.frame(x=S[[8]][["K"]], S[[8]][["ShearStress"]])
S8datapprox <- data.frame(approx(S8dat$x, S8dat$y, n = 1080))
str(S)
List of 8
 $ :'data.frame':   805 obs. of  3 variables:
  ..$ K           : num [1:805] 0 0.000498 0.000996 0.001494 0.001992 ...
  ..$ ShearStress : num [1:805] 0 178 356 578 841 ...
  ..$ NormalStress: num [1:805] 0 -1.77 -5.35 -7.14 -11 -15 -16.7 -20.4 -22 -23.6 ...
 $ :'data.frame':   1500 obs. of  3 variables:
  ..$ K           : num [1:1500] 0 0.0004 0.000801 0.001201 0.001602 ...
  ..$ ShearStress : num [1:1500] 0 23.6 38.3 43.7 68.3 ...
  ..$ NormalStress: num [1:1500] 0.1 -1.34 -2.49 -4.04 -5.7 -7.28 -9.08 -10.7 -12.5 -14.3 ...
 $ :'data.frame':   812 obs. of  3 variables:
  ..$ K           : num [1:812] 0 0.000419 0.000838 0.001257 0.001676 ...
  ..$ ShearStress : num [1:812] 0 243 547 973 1280 ...
  ..$ NormalStress: num [1:812] 0 -0.89 -3.63 -6.05 -8.7 -11.5 -14.1 -16.9 -19.3 -22.1 ...
 $ :'data.frame':   853 obs. of  3 variables:
  ..$ K           : num [1:853] 0 0.000587 0.001174 0.00176 0.002347 ...
  ..$ ShearStress : num [1:853] 0 246 480 756 1246 ...
  ..$ NormalStress: num [1:853] 0 1 3 3 4 ...
 $ :'data.frame':   916 obs. of  3 variables:
  ..$ K           : num [1:916] 0 0.000437 0.000874 0.001312 0.001749 ...
  ..$ ShearStress : num [1:916] 0 44.4 67.1 89.2 119.1 ...
  ..$ NormalStress: num [1:916] 0 -0.01 -2.08 -5.06 -7.06 -9.4 -11.9 -14.6 -17.2 -20.1 ...
 $ :'data.frame':   329 obs. of  3 variables:
  ..$ K           : num [1:329] 0 0.000213 0.000536 0.001105 0.001871 ...
  ..$ ShearStress : num [1:329] 0 52.7 174.7 415.4 740.5 ...
  ..$ NormalStress: num [1:329] 0 -29.1 -30.4 -31.8 -33 -34.2 -35.3 -36.4 -38.3 -39.8 ...
 $ :'data.frame':   790 obs. of  3 variables:
  ..$ K           : num [1:790] 0 0.000237 0.000745 0.001252 0.00176 ...
  ..$ ShearStress : num [1:790] 0 94.8 347.7 633.8 1215.9 ...
  ..$ NormalStress: num [1:790] 0 -6 -12 -17 -28.4 ...
 $ :'data.frame':   1060 obs. of  3 variables:
  ..$ K           : num [1:1060] 0 0.00051 0.00102 0.00153 0.00204 ...
  ..$ ShearStress : num [1:1060] 0 44.2 70.4 100.3 133.3 ...
  ..$ NormalStress: num [1:1060] 0 0.1 0.18 -0.2 -1.2 ... ````

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

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

发布评论

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

评论(1

你的呼吸 2025-01-23 00:20:29

我们可以使用 lapply 循环遍历 list 'S',而不是单独执行此操作并创建多个对象,

do.call(rbind, lapply(S, function(x) {
        x1 <- approx(x[["K"]], x[["ShearStress"]], n = nrow(x))$y
        data.frame(Mean = mean(x1, na.rm = TRUE), SD = sd(x1, na.rm = TRUE))
   }))

如果我们需要按元素 mean/sd, 然后我们需要将输出的length更改为listmax长度,然后将rowMeans/rowSds

lst1 <- lapply(S, function(x) {
        approx(x[["K"]], x[["ShearStress"]], n = nrow(x))$y
    })
mx <- max(lengths(lst1))
m1 <- as.matrix(do.call(cbind, lapply(lst1, `length<-`, mx)))
library(matrixStats)
out <- data.frame(Mean = rowMeans(m1, na.rm = TRUE),
                  SD = rowSds(m1, na.rm = TRUE))

Instead of doing this separately and creating multiple objects, we may loop over the list 'S' with lapply

do.call(rbind, lapply(S, function(x) {
        x1 <- approx(x[["K"]], x[["ShearStress"]], n = nrow(x))$y
        data.frame(Mean = mean(x1, na.rm = TRUE), SD = sd(x1, na.rm = TRUE))
   }))

If we need elementwise mean/sd, then we need to change the length of the output to the max length of the list and then rowMeans/rowSds

lst1 <- lapply(S, function(x) {
        approx(x[["K"]], x[["ShearStress"]], n = nrow(x))$y
    })
mx <- max(lengths(lst1))
m1 <- as.matrix(do.call(cbind, lapply(lst1, `length<-`, mx)))
library(matrixStats)
out <- data.frame(Mean = rowMeans(m1, na.rm = TRUE),
                  SD = rowSds(m1, na.rm = TRUE))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文