简体   繁体   中英

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.

This is the data at the moment but it still is not giving me 1 cm averages. 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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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