Dear R users,
I have a matrix composed of lists:
m <- matrix( list(), nrow=1, ncol=3 )
m[[ 1, 1 ]] <- list("A", "B")
m[[ 1, 2 ]] <- list("A", "C")
m[[ 1, 3 ]] <- list("A", "B")
and want to get the sub-matrix where cells contain "B".
But
m[ , "B" %in% m[ 1, ], drop=F ]
as well as
m[ , "B" %in% m[ 1, ][], drop=F ]
return empty matrices.
Any ideas, hints and help will be very much appreciated!
Kind regards!
Josef
[[alternative HTML version deleted]]
HI,
Does this work for you?
?mapply(function(x) x=="B",m)
????? [,1]? [,2]? [,3]
#[1,] FALSE FALSE FALSE
#[2,]? TRUE FALSE? TRUE
A.K.
----- Original Message -----
From: Asis Hallab <asis.hallab at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Tuesday, December 4, 2012 4:16 PM
Subject: [R] How to find matching columns in a matrix of lists?
Dear R users,
I have a matrix composed of lists:
m <- matrix( list(), nrow=1, ncol=3 )
m[[ 1, 1 ]] <- list("A", "B")
m[[ 1, 2 ]] <- list("A", "C")
m[[ 1, 3 ]] <- list("A", "B")
and want to get the sub-matrix where cells contain "B".
But
m[ , "B" %in% m[ 1, ], drop=F ]
as well as
m[ , "B" %in% m[ 1, ][], drop=F ]
return empty matrices.
Any ideas, hints and help will be very much appreciated!
Kind regards!
Josef
??? [[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.
Hello,
Try the following.
m[ , sapply(1:ncol(m), function(j) sapply("B", `%in%`, m[[1 , j]])),
drop=F ]
Hope this helps,
Rui Barradas
Em 04-12-2012 21:16, Asis Hallab escreveu:> Dear R users,
>
> I have a matrix composed of lists:
>
> m <- matrix( list(), nrow=1, ncol=3 )
> m[[ 1, 1 ]] <- list("A", "B")
> m[[ 1, 2 ]] <- list("A", "C")
> m[[ 1, 3 ]] <- list("A", "B")
>
> and want to get the sub-matrix where cells contain "B".
> But
>
> m[ , "B" %in% m[ 1, ], drop=F ]
>
> as well as
>
> m[ , "B" %in% m[ 1, ][], drop=F ]
>
> return empty matrices.
>
> Any ideas, hints and help will be very much appreciated!
> Kind regards!
> Josef
>
> [[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.
Hi R users following this thread!
I evaluated both solutions given by Rui and Arun.
Both work very well.
Arun's is a little faster.
I did the following time measurements on a large matrix with 2 rows,
labelled "InterPro" and "GO", and 88664 columns, labelled
with protein IDs.
I was interested in selecting those columns (proteins) that share a certain
"GO" annotation for molecular function.
So here is the time evaluation:
* Rui's method:
system.time(annos[ , sapply( annos[ "GO", ], function(x) {
"GO:0005634"
%in% x } ), drop=F ] )
user system elapsed
1.068 0.012 1.079
or
system.time(annos[ , unlist( lapply( annos[ "GO", ], function(x) {
"GO:0005634" %in% x } ) ), drop=F ])
user system elapsed
0.876 0.000 0.880
* Arun's method:
system.time( annos[ , mapply( function(x){any(x=="GO:0005634")},
annos[
"GO", ] ), drop=F ] )
user system elapsed
0.808 0.012 0.826
Thank you Rui and Arun very much for your help and everyone else for your
attention!
Kind regards!
2012/12/5 arun <smartpink111@yahoo.com>
> HI,
> Just tweaking my code also gives the same result:
> m[,mapply(function(x) any(x=="B"),m),drop=F]
> # [,1] [,2]
> #[1,] List,2 List,2
> A.K.
>
>
>
> ----- Original Message -----
> From: Asis Hallab <asis.hallab@gmail.com>
> To: Rui Barradas <ruipbarradas@sapo.pt>; r-help@r-project.org
> Cc:
> Sent: Tuesday, December 4, 2012 5:26 PM
> Subject: Re: [R] How to find matching columns in a matrix of lists?
>
> Hello,
>
> m[ , sapply(1:ncol(m), function(j) sapply("B", `%in%`, m[[1 ,
j]])), drop=F
> > ]
> >
>
> It indeed does.
>
> Thank you very much!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
>
--
Asis Hallab
Rothehausstr. 6 - 12
50823 Köln
Skype: asis.hallab.cgn
Fest (Köln) 42346046
Mobil (O2) 0176 63370211
Fax 01212 - 5 - 30697106
[[alternative HTML version deleted]]