Hi All, very rusty in R.. my results get overwritten when try to store within the loop. This my code: ListS=unique(data$Spec) Stat<- numeric(0) for(i in 5){ SS=subset(data,data$Spec==ListS[i]) maxC<- which.max(SS$Cc) smoothC=mean(SS$Cc[maxC + c(-2:2)]) currC=tail(SS,1)$Cc Index=currC/smoothC Stat[i]=c(Stat, Index[i]) } Stat This is what I get: [1] NA NA NA NA NA I am obviously not indexing well here. Thanks!!! N [[alternative HTML version deleted]]
Hi Nico, A bit difficult to see what is happening without the data, but two suggestions: smoothC=mean(SS$Cc[maxC + c(-2:2)],na.rm=TRUE) ... Stat[i]<-Index Jim On Tue, Sep 22, 2015 at 7:36 PM, Nico Gutierrez <nico.gutierrezo at gmail.com> wrote:> Hi All, > > very rusty in R.. my results get overwritten when try to store within the > loop. This my code: > ListS=unique(data$Spec) > Stat<- numeric(0) > > for(i in 5){ > > SS=subset(data,data$Spec==ListS[i]) > maxC<- which.max(SS$Cc) > smoothC=mean(SS$Cc[maxC + c(-2:2)]) > currC=tail(SS,1)$Cc > Index=currC/smoothC > > Stat[i]=c(Stat, Index[i]) > } > Stat > > This is what I get: > > [1] NA NA NA NA NA > > > I am obviously not indexing well here. > > > Thanks!!! > > N > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Dear Nico Comment inline On 22/09/2015 10:36, Nico Gutierrez wrote:> Hi All, > > very rusty in R.. my results get overwritten when try to store within the > loop. This my code: > ListS=unique(data$Spec) > Stat<- numeric(0) > > for(i in 5){ >Is that what you meant? I would have expected something like 1:5> SS=subset(data,data$Spec==ListS[i]) > maxC<- which.max(SS$Cc) > smoothC=mean(SS$Cc[maxC + c(-2:2)]) > currC=tail(SS,1)$Cc > Index=currC/smoothC > > Stat[i]=c(Stat, Index[i]) > } > Stat > > This is what I get: > > [1] NA NA NA NA NA > > > I am obviously not indexing well here. > > > Thanks!!! > > N > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html