[R] sliding window analysis with rollapply

R. Michael Weylandt michael.weylandt at gmail.com
Wed Oct 17 14:51:22 CEST 2012


On Tue, Oct 16, 2012 at 11:17 PM, Wang, Li <li.wang at ttu.edu> wrote:
> Dear List members
>
> I want to do the sliding window analysis of some specific values. Here is my code:
>
> require(zoo)
> dat <- read.table("chr1.txt", header = TRUE, sep="\t")
> dat2 <- cbind(dat[1,3]) #The first column is also important. It represents the position of the site on the chromosome.
> TS <- zoo(c(dat2))

Two Style comments:

1) Perhaps you just want read.zoo()

2) No need to have the c() when you aren't combining things.

> a <- rollapply(TS, width=1000000, by=200000, FUN=mean, align="left") #The third column values should roll against the first column values. I might be wrong here with the code.

3) rollmean() directly could also work here, but I'm not at all sure
what you mean by "the third column values should roll against the
first column values"

> plot1 <- plot.default(a, col="red", type="l") #It returns me some errors for this command.

4) Generally better to call the generic plot() instead of the
specialized plot.default()

Michael

>
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In min(x) : no non-missing arguments to min; returning Inf
> 2: In max(x) : no non-missing arguments to max; returning -Inf
> 3: In min(x) : no non-missing arguments to min; returning Inf
> 4: In max(x) : no non-missing arguments to max; returning -Inf
>
> Part of the original data looks like as follows:
> pos    Fit    Fst    Fis
> 12794    0.928380160665041    -0.0877263098996843    0.934156378600823
> 12816    0.901040947621283    0.0382417096943425    0.897106109324759
> 12821    0.901040947621283    0.0382417096943425    0.897106109324759
> 12827    0.901040947621283    0.0382417096943425    0.897106109324759
> 12855    0.909446752933768    0.00347893012209298    0.909130624726955
> 13324    0.498125727088629    0.0975914515920549    0.443850267379679
> 13338    0.53827858671811    0.103194947279406    0.485148514851485
> 13339    -0.059306330783135    -0.0198369843236668    -0.038701622971286
> 13379    0.610641507226514    0.175889055559966    0.527541169789892
> 13381    0.144593428517224    -0.0343016958072985    0.172962226640159
> 13390    0.216526396327467    0.179801071155318    0.0447761194029849
> 13454    0.498125727088629    0.0975914515920549    0.443850267379679
> 13457    0.309860615307135    -0.0501399771889025    0.342812006319115
> 13462    0.536517915763086    -0.0302077737766018    0.550108147080029
>
> That is, Fst against pos.
>
> I am very willing to be educated concerning the mistakes I made for the code.
>
> All the best
> Li
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list