问题描述
我创建了一个名为col的尺寸为150x4的矩阵.
I have created a matrix called col of dimension 150x4.
现在,我想通过绑定(col,col1,....,col149)创建一个150x600的矩阵,其中colk是通过将col的前k行替换为零来创建的.
Now I want to create a 150x600 matrix by binding (col,col1,....,col149), where colk is created by replacing the first k rows of col by zeros.
我的col1代码是
c1<-col[-1,] row<-(0,0,0,0) col1<-rbind(row,c1)
c1<-col[-1,] row<-(0,0,0,0) col1<-rbind(row,c1)
但是,如果k = 149,这将非常困难.我猜需要一个循环功能.另外,我创建的col1看起来不像矩阵.当我在R中打印时,没有行号.
However, It will be really hard if If k=149. I guess a loop function is required. Also, col1 that I created doesn't look like a matrix. When I print it in R, there's no row numbers.
由于我是R语言的新手,所以不知道如何在R语言中做这些事情.
Since I am new to R, don't know how to do these things in R.
我的col代码是col< -t(res),可以在如何从给定向量,使用for循环
My code for col is col<-t(res),which can be seen inHow to make a matrix from a given vector by using for loop
有人可以帮我解决这个问题吗?提前致谢 !
Can anyone help me address this issue? Thanks in advance !
推荐答案
可能有帮助:
res1 <- cbind(colN,do.call(`cbind`, lapply(1:149, function(i) {
rN <- matrix(rowN[col(colN[1:i,,drop=FALSE])], ncol=4)
rbind(rN, colN[-(1:i),])
})))
dim(res1)
#[1] 150 600
数据
通过链接
data
From the link
set.seed(25)
x<-runif(150,-2,2)
x1<-c(0,0,0,0,x)
n <- length(x1)-1
res <- sapply((4:n)-3, function(i) x1[(i+3):i])
colN <- t(res)
rowN <- rep(0,4)
这篇关于通过使用循环功能删除矩阵的k个第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!