我是R
的初学者,需要以下序列代码的帮助:
set.seed(2010)
A <- matrix(sample(18),nrow=6)
如何获得所有行但只有奇数列?
由于
答案 0 :(得分:1)
您可以使用A[,c(TRUE,FALSE)]
:
A
# [,1] [,2] [,3]
#[1,] 6 16 2
#[2,] 9 1 3
#[3,] 8 5 17
#[4,] 4 11 18
#[5,] 12 14 10
#[6,] 13 7 15
A[,c(T,F)] # the first and third columns are picked up due to cycling
# [,1] [,2]
#[1,] 6 2
#[2,] 9 3
#[3,] 8 17
#[4,] 4 18
#[5,] 12 10
#[6,] 13 15
答案 1 :(得分:0)
使用A[, seq(1,ncol(A), by = 2)]
# [,1] [,2]
#[1,] 6 2
#[2,] 9 3
#[3,] 8 17
#[4,] 4 18
#[5,] 12 10
#[6,] 13 15
直接选择行的另一个选项:
tools:src