简体   繁体   English

滚动平均值 function 类似于动物园中的 pandas

[英]Rolling mean function similar to pandas in zoo

I want to create a rolling mean of some geochemical data.我想创建一些地球化学数据的滚动平均值。 Currently, I have data for every 1 mm from 0mm to 45.7 mm and I want to average every 10 mm to create 1 cm averages.目前,我有从 0 毫米到 45.7 毫米的每 1 毫米的数据,我想平均每 10 毫米以创建 1 厘米的平均值。

This is the data at the moment but it still is not giving me 1 cm averages.这是目前的数据,但它仍然没有给我 1 厘米的平均值。 Can someone tell me where I am going wrong?有人可以告诉我哪里出错了吗? Thanks谢谢


wapPbTa<-read.csv("wappbta.csv",header=TRUE)

wapPbTa<-wapPbTa[-c(251:458), ] 

library(ggplot2)
library(tidypaleo)
library(zoo)
width <- 10

RM<-rollmean(x = wapPbTa$PbTa, k = width,fill=NA)

##Averaged data

ggplot(wapPbTa, aes(x =RM , y = Depth))+
  labs(y = "Depth (cm)")+
  geom_lineh(size=1)+
  geom_point(size=2)+
  theme_classic()+
  scale_y_reverse()

## Unaveraged data
ggplot(wapPbTa, aes(x =PbTa , y = Depth))+
  labs(y = "Depth (cm)")+
  geom_lineh(size=1)+
  geom_point(size=2)+
  theme_classic()+
  scale_y_reverse()

structure(list(Depth = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 
0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9), PbTa = c(0.163857678, 
0.161569533, 0.086305592, 0, 0.006086142, 0, 0, 0.044096031, 
0.050739958, 0.088385995, 0.104100946, 0.133012821, 0, 0.127524872, 
0.046368715, 0.02514558, 0.109383676, 0.081979695, 0.0766503, 
0.064679583)), row.names = c(NA, 20L), class = "data.frame")

This type of problems generally has to do with reshaping the data.这类问题通常与重塑数据有关。 The format should be the long format and the data is in wide format.格式应为长格式,数据为宽格式。 See this post on how to reshape the data from wide to long format.请参阅这篇文章,了解如何将数据从宽格式重塑为长格式。

library(ggplot2)
library(dplyr)
library(tidyr)
library(tidypaleo)
library(zoo)

width <- 10
wapPbTa$RM <- rollmeanr(x = wapPbTa$PbTa, k = width, fill = NA)

wapPbTa %>%
  pivot_longer(cols = -Depth) %>%
  ggplot(aes(x = value, y = Depth, colour = name)) +
  geom_lineh(size = 1) +
  geom_point(size = 2) +
  scale_y_reverse() +
  scale_colour_manual(
    breaks = c("PbTa", "RM"),
    values = c("black", "blue")
  ) +
  labs(y = "Depth (cm)") +
  theme_classic()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM