大神们,小弟问下R语言中向量变矩阵问题。

大神们,小弟问下R语言中向量变矩阵问题。我有个向量n=(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)现在想变成这样的矩阵{(x1,x2,x3)(x2,x3,x4)(x3,x4,x5)...(x8,x9,x10)}怎么弄啊?谢谢大神。

第1个回答  2016-09-02
m<-matrix(n[1:3],n[2:4],n[3:5],n[4:6],n[5:7],n[6:8],n[7:9],n[8:10],nrow=3)
第2个回答  2019-09-19
m<-matrix(n[1:3],n[2:4],n[3:5],n[4:6],n[5:7],n[6:8],n[7:缉常光端叱得癸全含户9],n[8:10],nrow=3)
第3个回答  2016-09-02
举例说明
index <- NULL
for(i in 1:8){
index <- c(index,i:(i+2))
}
> index
[1] 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 9 8 9 10
> ( n <- paste0("x",1:10) )
[1] "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"
> ( matrix(n[index],ncol=3,byrow=T) )
[,1] [,2] [,3]
[1,] "x1" "x2" "x3"
[2,] "x2" "x3" "x4"
[3,] "x3" "x4" "x5"
[4,] "x4" "x5" "x6"
[5,] "x5" "x6" "x7"
[6,] "x6" "x7" "x8"
[7,] "x7" "x8" "x9"
[8,] "x8" "x9" "x10"
是要这样的效果吗本回答被提问者和网友采纳
相似回答