[R] rbind ing matrices and resetting column numbers
David Winsemius
dwinsemius at comcast.net
Thu Oct 14 16:49:34 CEST 2010
On Oct 14, 2010, at 10:40 AM, David Winsemius wrote:
>
> On Oct 14, 2010, at 10:31 AM, Maas James Dr (MED) wrote:
>
>> Sorry for the verbose example. I want to row bind two matrices,
>> and all works except I want the column labelled "row" to be
>> sequential in the new matrix, shown as "mat3" here, i.e. needs to
>> be 1:6 and not 1:3 repeated twice. Any suggestions?
>
> cbind( row=1:sum(nrow(mat1),nrow(mat2)),
> rbind(mat1[ ,-1],mat2[ ,-1])
> )
NOte that using row as a column name could be a bit confusing down the
line for two reason: a) it's actually a column, and b) "row" is the
name of a useful function. In fact, that function could be used to
solve your request using your mat3 constructed below:
> mat3[ , 1] <- row(mat3)[ , 1]
> mat3
row ti counti
[1,] 1 7 9
[2,] 2 8 5
[3,] 3 7 9
[4,] 4 5 3
[5,] 5 6 8
[6,] 6 3 7
>
> --
> David.
>>
>> Thanks
>>
>> J
>>
>>> colnm1 <- c("row","ti","counti")
>>> colnm2 <- c("row","tj","countj")
>>> mat1 <- matrix(c(1,7,9,2,8,5,3,7,9),byrow=T,nrow=3)
>>> colnames(mat1) <- colnm1
>>> mat1
>> row ti counti
>> [1,] 1 7 9
>> [2,] 2 8 5
>> [3,] 3 7 9
>>> mat2 <- matrix(c(1,5,3,2,6,8,3,3,7),byrow=T,nrow=3)
>>> colnames(mat2) <- colnm2
>>> mat2
>> row tj countj
>> [1,] 1 5 3
>> [2,] 2 6 8
>> [3,] 3 3 7
>>> mat3 <- rbind(mat1,mat2)
>>> mat3
>> row ti counti
>> [1,] 1 7 9
>> [2,] 2 8 5
>> [3,] 3 7 9
>> [4,] 1 5 3
>> [5,] 2 6 8
>> [6,] 3 3 7
>>
>>
>> ===============================
>> Dr. Jim Maas
>> University of East Anglia
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list