Question

I have a text file with data like this

4924    0.0000 
4989    0.0287 
4702    0.0574 
4169    0.0861 
3616    0.1148 
3148    0.1435 
2682    0.1722 
2314    0.2010 
2153    0.2297 
2079    0.2584 
2019    0.2871 
2060    0.3158 
2314    0.3445 
2472    0.3732 
2604    0.4019 
2904    0.4306 
3173    0.4593 
3392    0.4880 
3655    0.5167 
3862    0.5455 
4101    0.5742 
4441    0.6029 
4625    0.6316 
4698    0.6603

I would like to perform a kernel smooth on Column 1 (y-axis) before plotting vs Column 2.

The desired smooth is: result = {[1 x prev result + 2x current result + [1 x next result] / 4}

i.e. a standard 1-2-1 smooth in some imaging fields, just purely on a single column/curve.

Can someone suggest if there is an existing R package that I can use to either make my own kernel, or if this simple kernel exists?

My other though was maybe to do in perl (though I am a beginner in perl...) but in R would be more satisfactory I think.

Was it helpful?

Solution

Like this?

DF <- read.table(text="4924    0.0000 
4989    0.0287 
4702    0.0574 
4169    0.0861 
3616    0.1148 
3148    0.1435 
2682    0.1722 
2314    0.2010 
2153    0.2297 
2079    0.2584 
2019    0.2871 
2060    0.3158 
2314    0.3445 
2472    0.3732 
2604    0.4019 
2904    0.4306 
3173    0.4593 
3392    0.4880 
3655    0.5167 
3862    0.5455 
4101    0.5742 
4441    0.6029 
4625    0.6316 
4698    0.6603")

DF[, 3] <- filter(DF[, 1], c(1, 2, 1)/4)

plot(DF[, 2:1])
lines(DF[, 2:3])

enter image description here

OTHER TIPS

Not sure if it's standard anywhere, how about a simple function?

# getting your data
dat<-read.csv("k.csv",header=F)
dat

# function (replicates rather than extrapolates endpoints)
ksm<-function(vec){
  l<-length(vec)
  svec<-c(vec[1],vec,vec[l])
  return(((vec*2)+svec[1:l]+svec[3:(l+2)])/4)
}

plot(dat$V2,ksm(dat$V1))

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top