[R] While loop

Henrique Dallazuanna wwwhsd at gmail.com
Fri Jul 11 19:55:49 CEST 2008


If I understand correctly, you can try:

m <- matrix(sample(0:5, 24, rep = TRUE), nc = 3)

#  A list with the index
lapply(apply(m == 0, 2, which), head, 1)

# A vector
unlist(lapply(apply(m == 0, 2, which), head, 1))

On Fri, Jul 11, 2008 at 2:06 PM, Rheannon <rbrooks at uvic.ca> wrote:
>
> Hello,
>
> I am trying to loop through a matrix column and find the first value <=0 and
> store that matrix location in a variable called Start.
>
> I have tried the following:
> i <- 1
> j <- 1
> while (Matrix[i, j] > 0)
> i = (i + 1)
> #loop until matrix [i, j] value <= 0
> #strore that row number in a variable
> Start <- i
>
> I am getting a scripting out of bounds error and I suspect maybe the loop
> isnt stoping at the end of the matrix?
>
> Any help is greatly appreciated!
> Cheers,
> Rheannon
>
> --
> View this message in context: http://www.nabble.com/While-loop-tp18408462p18408462.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list