[R] How to find matching columns in a matrix of lists?

Rui Barradas ruipbarradas at sapo.pt
Tue Dec 4 23:20:58 CET 2012


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.




More information about the R-help mailing list