[R] help with matrix column cleaning
David Winsemius
dwinsemius at comcast.net
Tue Feb 14 07:40:34 CET 2012
On Feb 14, 2012, at 12:46 AM, Vijayan Padmanabhan wrote:
> Dear R Group
> I have a matrix object in R, where i want to retain only those columns
> which have each row from a different group..
>
> see the example below..
> team1<-c("a1","a2","b1","b2","b3","c1","c2")
>
> teams<-combn(team1, 3)
>
> I want a function which will delete all columns in the teams matrix
> that
> have more than 1 row having same letter starts..
>
> For instance in the example object teams, I want to retain column
> 8,11,12,13,14,18,19,21,22,23,24 .
This will give you a logical vector which you can use with "[" in the
column position to select those columns:
apply(teams, 2, function(x) length( unique(substr(x, 1,1))) ==3 )
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
TRUE TRUE
[13] TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE
TRUE TRUE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> I am looking at doing this in larger matrix objects using a function
> preferrably that i can apply to the matrix object to check for the
> beginning letter of each cell from each row and delete columns that
> have
> more than one row having the same start letter.
>
> Thanks for your help.
> Regards
> Vijayan Padmanabhan
>
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list