[R] fill up a matrix

Jean Eid jeaneid at chass.utoronto.ca
Thu Apr 22 14:45:43 CEST 2004


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
>




More information about the R-help mailing list