[R] adding a column to a matrix

Vladimir Eremeev wl2776 at gmail.com
Thu Apr 26 11:48:32 CEST 2007


m<-cbind(m,0)
m[m[,3]>m[,4],5]<-1
colnames(m)[5]<-"censoring"


raymond chiruka wrote:
> 
> i would like to add a variable to an existing matrix by manipulating 2
> previous variables eg for the data
>   m
>          treat strata      censti     survTime
>     [1,]     1      2  284.684074  690.4961005
>     [2,]     1      1  172.764515   32.3990335
>     [3,]     1      1 2393.195400   24.6145279
>     [4,]     2      1   30.364771    8.0272267
>     [5,]     1      1  523.182282  554.7659501
>   
>   l would want to add a new column censoring by comparing ( if censti >
> survtime then censoring=1) how do l go about it
>   thanks
>   
>   the code l used to generate the data is
>    n=100
>   > m=matrix(nrow=n,ncol=4)
>   > colnames(m)=c("treat",  "strata", "censti", "survTime")
>   > for(i in 1:100)
> m[i,]=c(sample(c(1,2),1,replace=TRUE),sample(c(1,2),1,replace=TRUE),rexp(1,.002),rexp(1,.005))
>   > m
>   
>   l know its ugly but it seems to work 
>   any suggestions l still new at this
>   thanks
> 
> 

-- 
View this message in context: http://www.nabble.com/adding-a-column-to-a-matrix-tf3650601.html#a10197380
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list