[R] Write vector/matrix in a loop

Ista Zahn izahn at psych.rochester.edu
Tue Aug 16 19:53:24 CEST 2011


Hi,

On Tue, Aug 16, 2011 at 1:31 PM, kokavolchkov <kokavolchkov at gmail.com> wrote:
> Hello!
>
> I'm trying to *save values* (*row and col*) in two matrices(1,m) or vectors.
> I have a loop, where a get these values:
>
> for(i in 144){
>        for(j in 73){
>                if(B4[i,j]==1){ # B4 is a matrix(73, 144)
>                        row <- B4[i-(i-1),j]
>                        col <- B4[i,j-(j-1)]
>                }
>        }
> }
>
> *How to save row and col?*

Create an object to hold them, and use indexing to assign individual values:

rows <- vector()
cols <- vector()

for(i in 144){
       for(j in 73){
               if(B4[i,j]==1){ # B4 is a matrix(73, 144)
                       rows[i] <- B4[i-(i-1),j]
                       cols[i] <- B4[i,j-(j-1)]
               }
       }
}

Best,
Ista

>
> Thank you!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Write-vector-matrix-in-a-loop-tp3747862p3747862.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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list