简体   繁体   中英

Matrix Loop in R

I am trying to do a simple for matrix loop in R. But for some reason it is not working. Any help would be great! Here is what I have:

        t<-length(gdat2)
        > t
        [1] 6848
        for (i in 2:t)

        {F1[i,]=F1[i-1,]}

       > F1
     [,1]     [,2]
     [1,] 7.494972 17.04625

Where F1 is a 1x2 matrix.

EDIT: Okay. I changed it. Thanks all for the responses! And I edited my code to read 'i' instead of 't'...I found out that I do not have a broken keyboard :).

So, what I have is a F1 matrix with the first row written. NOW, I want to create a new matrix where all the subsequent rows (rows 2 and onward) will be written based on the F1 matrix. I should have 6848 rows in the new matrix. For instance, row 2 should read: 7.494972 17.04625 row 3 should read: 7.494972 17.04625 .... row 6848 should read: 7.494972 17.04625 Hope this clears everything up.

if you really just want to copy the first line of the matrix 6848 times, this code will do the work:

for (i in 2:t){
F1<-rbind(F1, F1[i-1,])
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM