I'm new here and all. I was just browsing through some of the questions and found this code that I dont understand and might be of use to me....They downloaded an IRanges package in R and the code has something to do with intervals.
Asked
Active
Viewed 169 times
0
-
In R, simply type `help(data.frame)` and run it to see what the help file says. – Tommy O'Dell Mar 05 '13 at 02:50
-
1The code is a little be complicated. But it computes the mean of column2 over intervals of column1. (col4 over intervals col3).... I think we can avoid the use of Iranges here( by a clever cut) to get the same result. thats 's said, your question is really bad. You don't give data to help us to test this fuction. I am pretty sure you pick up this code from SO. So i downvote this question because I don't see any effort from your side. – agstudy Mar 05 '13 at 03:07
-
As I said in the previous comment , the code of this question , is [here](http://stackoverflow.com/questions/15041684/r-programming-help-in-editing-code). – agstudy Mar 05 '13 at 03:15
-
Why did you try to edit my answer ( or to remove it )? – agstudy Mar 07 '13 at 10:29
1 Answers
0
for more information about tapply
see the ?tapply.
Here a version of the code using cut
and not the package IRanges:
idx <- seq(1, ncol(df), by=2)
o1 <- lapply(idx, function(i) {
## create grouping factor
fac <- cut(df[,i],seq(0,max(df[i]),30),labels=F)
fac[is.na(fac)] <- max(fac,na.rm=T)+1
# compute the mean by interval
mean=tapply(df[,i+1],fac, mean)
# put the result in a data.frame
fac=levels(as.factor(fac))
d <- data.frame(mean=mean,fac=fac)
})
Applying this on this structure:
[1]]
mean fac
1 1.300000 1
2 1.450000 2
3 2.925000 3
4 1.700000 4
5 2.333333 5
[[2]]
mean fac
1 2.500000 1
2 2.350000 2
3 1.516667 3
[[3]]
mean fac
1 1.78 1
2 1.90 2