user1786747
user1786747

Reputation: 85

rollapply sliding window

I am very much a newbie in R. I am sorry for my naive question.

Part of my data is as follows:

POS         Obs_FCT
4345     0.10049191
4484     0.08930793
4515    -0.00468725
4547    -0.00749802
4628     0.01143598
5347     0.05673895
6071     0.01143598
6449    -0.00070778
6498    -0.0109962
7320     0.00543984
7457    -0.00206247
7761     0.08018589
7875    -0.00601304
7988    -0.00070778
9459     0.01447144
9483     0.08269899
9495    -0.03353229
9552    -0.00206247
9602    -0.0269909
9701    -0.00206247
11809   -0.01952417
12593   -0.02173826
16787    0.08930793
17049   -0.06738125
17058    0.01325792

I want to plot Obs_FCT (which is the Y-axis) VS POS (the X-axis), but it is not plotting at all. I also want to apply a sliding window on the POS column. The window is as follows (i.e. that the window size is 1000 and sliding by 200):

1--1000
200 ---1200
400 ---1400 
600 ----1600
--------

If the numbers in the POS column are located in each window, compute the mean value of Obs_FCT. (The X axis uses the midpoint of the window).

Could someone please tell me how to use R code to achieve this? Generally I know that maybe I can get what I want through rollapply. But it seems like a function is needed.

Upvotes: 3

Views: 810

Answers (1)

zelite
zelite

Reputation: 1488

I copypasted your data into a text file and read it into R with read.table. Using the plot command gives the results you want plot

> d<-read.table("path/to/file.txt", header=TRUE, sep="")
> head(d)
   POS     Obs_FCT
1 4345  0.10049191
2 4484  0.08930793
3 4515 -0.00468725
4 4547 -0.00749802
5 4628  0.01143598
6 5347  0.05673895
>plot(d)

This makes a plot with POS in X axis and Obs_FCT in Y axis.

runmean from package caTools, lets you make a running median. You can specify the number of points in the window, but you can not specify how you want the window to slide.

> md <- runmean(x=d$Obs_FCT, k=1000)

You can then plot:

plot(d$POS, md)

Upvotes: 1

Related Questions