大神们,小弟问下R语言中向量变矩阵问题。
大神们,小弟问下R语言中向量变矩阵问题。我有个向量n=(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)现在想变成这样的矩阵{(x1,x2,x3)(x2,x...
大神们,小弟问下R语言中向量变矩阵问题。我有个向量n=(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)现在想变成这样的矩阵{(x1,x2,x3)(x2,x3,x4)(x3,x4,x5)...(x8,x9,x10)}怎么弄啊?谢谢大神。
展开
展开全部
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)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
举例说明
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"
是要这样的效果吗
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"
是要这样的效果吗
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询