Hi all, I want to fill in this matrix vectors (by column) without overwriting the first elements in column 1995. Is there any other way than concatenate the first element with the vector and then assign this new vector to the column in matrix? matrix[,"3"]<-c(1591,"vector") matrix[,"4"]<-c(405,"vector") ... ... matrix 2 3 4 5 6 7 8 9 10 1995 278 1591 405 482 285 99 220 48 4 1996 1220 NA NA NA NA NA NA NA NA 1997 3106 NA NA NA NA NA NA NA NA 1998 1895 NA NA NA NA NA NA NA NA 1999 1376 NA NA NA NA NA NA NA NA 2000 565 NA NA NA NA NA NA NA NA 2001 491 NA NA NA NA NA NA NA NA 2002 1169 NA NA NA NA NA NA NA NA 2003 2310 NA NA NA NA NA NA NA NA Luis Ridao Cruz Fiskiranns??knarstovan N??at??n 1 P.O. Box 3051 FR-110 T??rshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: luisr at frs.fo Web: www.frs.fo
"Luis Rideau Cruz" <Luisr at frs.fo> writes:> Hi all, > > I want to fill in this matrix vectors (by column) without overwriting the first elements in column 1995. > > Is there any other way than concatenate the first element with the vector and then assign this new vector > to the column in matrix? > > matrix[,"3"]<-c(1591,"vector") > matrix[,"4"]<-c(405,"vector") > ... > ... > > matrix > 2 3 4 5 6 7 8 9 10 > 1995 278 1591 405 482 285 99 220 48 4 > 1996 1220 NA NA NA NA NA NA NA NA > 1997 3106 NA NA NA NA NA NA NA NA > 1998 1895 NA NA NA NA NA NA NA NA > 1999 1376 NA NA NA NA NA NA NA NA > 2000 565 NA NA NA NA NA NA NA NA > 2001 491 NA NA NA NA NA NA NA NA > 2002 1169 NA NA NA NA NA NA NA NA > 2003 2310 NA NA NA NA NA NA NA NAThere's a bit of formatting damage there, is this a matrix with rownames 1995:2003 and colnames 2:10? Have a look at this: m <- matrix(NA,9,9) m[,1] <- round(1e4*runif(9)) m[1,] <- round(1e4*runif(9)) colnames(m) <- 2:10 rownames(m) <- 1995:2003 m # should look like yours now m[-1,-1] <- 1:64 m -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Try this: m <- matrix(1:9, 3) m[-1,] <- matrix(11:16,2) Luis Rideau Cruz <Luisr <at> frs.fo> writes: : : Hi all, : : I want to fill in this matrix vectors (by column) without overwriting the first elements in column 1995. : : Is there any other way than concatenate the first element with the vector and then assign this new vector : to the column in matrix? : : matrix[,"3"]<-c(1591,"vector") : matrix[,"4"]<-c(405,"vector") : ... : ... : : matrix : 2 3 4 5 6 7 8 9 10 : 1995 278 1591 405 482 285 99 220 48 4 : 1996 1220 NA NA NA NA NA NA NA NA : 1997 3106 NA NA NA NA NA NA NA NA : 1998 1895 NA NA NA NA NA NA NA NA : 1999 1376 NA NA NA NA NA NA NA NA : 2000 565 NA NA NA NA NA NA NA NA : 2001 491 NA NA NA NA NA NA NA NA : 2002 1169 NA NA NA NA NA NA NA NA : 2003 2310 NA NA NA NA NA NA NA NA : : : Luis Ridao Cruz : Fiskiranns伱伋knarstovan : N伱伋at伱伜n 1 : P.O. Box 3051 : FR-110 T伱伋rshavn : Faroe Islands : Phone: +298 353900 : Phone(direct): +298 353912 : Mobile: +298 580800 : Fax: +298 353901 : E-mail: luisr <at> frs.fo : Web: www.frs.fo : : ______________________________________________ : R-help <at> stat.math.ethz.ch mailing list : https://www.stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : :
you can do this, tmpmatrix <- matrix(unlist((lapply(as.data.frame(x[, 3:ncol(x)]), function(t) return(rep(t[!is.na(t)], 9))) )), byrow=F, ncol=8) matrix <- cbind(matrix[, 1:2], tmpmatrix) On Thu, 22 Apr 2004, Luis Rideau Cruz wrote:> Hi all, > > I want to fill in this matrix vectors (by column) without overwriting the first elements in column 1995. > > Is there any other way than concatenate the first element with the vector and then assign this new vector > to the column in matrix? > > matrix[,"3"]<-c(1591,"vector") > matrix[,"4"]<-c(405,"vector") > ... > ... > > matrix > 2 3 4 5 6 7 8 9 10 > 1995 278 1591 405 482 285 99 220 48 4 > 1996 1220 NA NA NA NA NA NA NA NA > 1997 3106 NA NA NA NA NA NA NA NA > 1998 1895 NA NA NA NA NA NA NA NA > 1999 1376 NA NA NA NA NA NA NA NA > 2000 565 NA NA NA NA NA NA NA NA > 2001 491 NA NA NA NA NA NA NA NA > 2002 1169 NA NA NA NA NA NA NA NA > 2003 2310 NA NA NA NA NA NA NA NA > > > Luis Ridao Cruz > Fiskiranns侒knarstovan > N侒at侜n 1 > P.O. Box 3051 > FR-110 T侒rshavn > Faroe Islands > Phone: +298 353900 > Phone(direct): +298 353912 > Mobile: +298 580800 > Fax: +298 353901 > E-mail: luisr at frs.fo > Web: www.frs.fo > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >