几乎每天都有人再次对我进行指控:
m <- matrix( 1:6, ncol=2 )
while( dim(m)[1] > 0 ){
print(m);
m <- m[-1,]
}
gives:
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
[,1] [,2]
[1,] 2 5
[2,] 3 6
Error in while (dim(m)[1] > 0) { : argument is of length zero
R是否与1xn matrices有问题,或我的错误在哪里?
> nrow( m[-c(2,3), ] )
NULL
> dim( m[-c(2,3), ] )
NULL
> m[-c(2,3), ][,1]
Error in m[-c(2, 3), ][, 1] : incorrect number of dimensions
> str( m[-c(2,3), ] )
int [1:2] 1 4
Any idea how to easily fix the initial example, which is close to my actual problem? BTW: This loop is the bottleneck of my algorithm. Hence, efficient solutions are appreciated.
感谢!