Hello,
I have a simple question: I want to list numbers 1:k, but if k <1, I hope
nothing listed.
how should we do?
k=2
for (i in 1:k) print(i)
[1] 1 # <-correct
[1] 2
k=0
for (i in 1:k) print(i)
[1] 1 #<---- wrong
[1] 0
thanks
jian
[[alternative HTML version deleted]]
I've been caught out by this more times than I care to admit -
forgetting that an R for loop isn't a C for loop.
Here's one solution...
k <- start.value
while (k <= end.value) {
# do stuff
k <- k + 1
}
Michael
On 11 September 2010 18:39, Yuan Jian <jayuan2008 at yahoo.com>
wrote:> Hello,
> I have a simple question: I want to list numbers 1:k, but if k <1, I
hope nothing listed.
> how should we do?
> k=2
> for (i in 1:k)? print(i)
> [1] 1? ? # <-correct
> [1] 2
> k=0
> for (i in 1:k) print(i)
> [1] 1????? #<---- wrong
> [1] 0
>
>
>
> thanks
> jian
>
>
>
>
>
> ? ? ? ?[[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.
>
>
On Sat, Sep 11, 2010 at 4:39 AM, Yuan Jian <jayuan2008 at yahoo.com> wrote:> Hello, > I have a simple question: I want to list numbers 1:k, but if k <1, I hope nothing listed. > how should we do? > k=2 > for (i in 1:k)? print(i) > [1] 1? ? # <-correct > [1] 2 > k=0 > for (i in 1:k) print(i) > [1] 1????? #<---- wrong > [1] 0 >Try: k <- 0 for (i in seq_len(k)) print(i) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
or: k=0 for (i in 1:k) if(k>0) print(i) -- View this message in context: http://r.789695.n4.nabble.com/for-loop-tp2535626p2535640.html Sent from the R help mailing list archive at Nabble.com.
Hi guys,
Im new to R and am having a bit of trouble with what should be a simple loop. It
sprobably something very fundamental that im doing wrong.
for(i in c(1:520))
{
tmp1<- ((1-samp.pct[i])^2)*(log(1-theor.pct[i])-log(1-theor.pct[i+1]))
tmp2<- ((samp.pct[i])^2)*(log(theor.pct[i+1])-log(theor.pct[i]))
}
ADtest.stat<- (-n*(max(theor.pct))) + (n*sum(tmp1)) + (n*sum(tmp2))
names(ADtest.stat) <- c("Anderson-Darling test statistic")
print(ADtest.stat)
samp.pct is a vector containing 520 values between 0 and 1, so is theor.pct. n
is 520.
After running this code, tmp1 and tmp2 return only one value, where I need
around 520 values. Any ideas would be appreciated.
Regards,
Andre
[[alternative HTML version deleted]]
First up, you've got a problem if your vectors are 520 elements in length because you accessing element i+1 in your loop and when i is 520 you won't have a valid value. Next, you don't really need a for loop at all :) ?You can do those operations on the whole vectors... e.g. tmp1 <- (1 - samp.pct[-n)^2 * diff(log(1 - theor.pct)) (just replaced your explicit subtraction of lagged values with diff) Does that help ? Michael Note, in the line above, the last element is omitted with "[-n]". See help page on the diff function if you are not familiar with it. On 21 September 2010 16:41, andre bedon <andresago1 at hotmail.com> wrote:> > Hi guys, > > Im new to R and am having a bit of trouble with what should be a simple loop. It sprobably something very fundamental that im doing wrong. > > > > for(i in c(1:520)) > { > tmp1<- ((1-samp.pct[i])^2)*(log(1-theor.pct[i])-log(1-theor.pct[i+1])) > tmp2<- ((samp.pct[i])^2)*(log(theor.pct[i+1])-log(theor.pct[i])) > } > ADtest.stat<- (-n*(max(theor.pct))) + (n*sum(tmp1)) + (n*sum(tmp2)) > names(ADtest.stat) <- c("Anderson-Darling test statistic") > print(ADtest.stat) > > > > samp.pct is a vector containing 520 values between 0 and 1, so is theor.pct. n is 520. > > After running this code, tmp1 and tmp2 return only one value, where I need around 520 values. Any ideas would be appreciated. > > > > Regards, > > > > Andre > > > > ? ? ? ?[[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. >