loop within a loop in R -
i'm trying write loop within loop in r, seems inner loop working:
test <- rbind(c(1:30), c(31:60), c(61:90), c(91:120))
here's i've been written:
b<- data.frame() n<-1 i<-1 c<- data.frame() while(i<=4){ while(n<=10) { a<-cbind(test[i, 3*n-2], test[i, 3*n-1], test[i,3*n]) b<-rbind(b,a); n<-n+1 } ; i<-i+1; c<-cbind(c,b) }
basically i'm trying group each line 3 columns , transform multiple lines, , every line , bind these columns.
my desired output like:
1 2 3 31 32 33 61 62 63 81 82 83 4 5 6 34 35 36 64 65 66 84 85 86 7 8 9 37 38 39 67 68 69 ...... 10 11 12 40 41 42 ...... 13 14 15 43 45 46 16 17 18 ...... 19 20 21 22 23 24 25 26 27 28 29 30 58 59 60 78 79 80 118 119 120
however have written work first line, apparently outer loop doesn't work. what's wrong please?
edit: here's further tried in response loop:
b<- data.frame() c<- data.frame() for(i in seq(from = 1, = 4, =1)){ for( n in seq(from=1, to=10, by=1)) { <- cbind(test[i, 3*n-2], test[i, 3*n-1], test[i,3*n]) b <- rbind(b,a) } c <- cbind(c,b) }
but still not make work. what's wrong it?
this might make life bit easier...
out <- lapply( data.frame( t(test) ) , matrix , ncol = 3 , byrow = true ) res <- do.call( cbind , out ) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 1 2 3 31 32 33 61 62 63 91 92 93 [2,] 4 5 6 34 35 36 64 65 66 94 95 96 [3,] 7 8 9 37 38 39 67 68 69 97 98 99 [4,] 10 11 12 40 41 42 70 71 72 100 101 102 [5,] 13 14 15 43 44 45 73 74 75 103 104 105 [6,] 16 17 18 46 47 48 76 77 78 106 107 108 [7,] 19 20 21 49 50 51 79 80 81 109 110 111 [8,] 22 23 24 52 53 54 82 83 84 112 113 114 [9,] 25 26 27 55 56 57 85 86 87 115 116 117 [10,] 28 29 30 58 59 60 88 89 90 118 119 120
possibly easier do...
do.call( cbind , lapply( 1:4 , function(x) matrix( test[x,] , ncol = 3 , byrow = f ) ) )
if insist on using for
loop easiest way...
out <- numeric() for( in 1:4 ){ tmp <- matrix( test[ , ] , ncol = 3 , byrow = true ) out <- cbind( out , tmp ) }
the problem stemmed use of out <- data.frame
initialise result data structure. meant trying cbind
data.frame
no rows 1 10 rows. not possible (although intuitively being able cbind
empty vector 10 row matrix
should fail doesnt!).
Comments
Post a Comment