An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120220/1229d8e4/attachment.pl>
How to determine a subset of a binary strings?
4 messages · jing tang, Berend Hasselman, Enrico Schumann +1 more
On 20-02-2012, at 14:15, jing tang wrote:
Hi, I need some neat ways of determing a subset of binary strings. For example, x=c(0,0,1), y=c(0,1,1), z=c(0,1,0). So x is a subset of y and z is also a subset of y, but x is not a subset of z. I tried to search R functions and packages but no hits. Any ideas?
x <- c(0,0,1) y <- c(0,1,1) z <- c(0,1,0) any(x & y) any(z & y) any(x & z) Berend
Hi Jing,
I am not sure I got your definition of a "subset" right, but maybe this
helps.
Regards,
Enrico
x <- c(0,0,1)
y <- c(0,1,1)
z <- c(0,1,0)
## is x a 'subset' of y?
isSubset <- function(x, y) {
x <- as.logical(x)
y <- as.logical(y)
all(y[x] == TRUE)
}
isSubset(x, y)
isSubset(z, y)
isSubset(x, z)
## create all subsets
exampleVec <- c(0,1,1,0,1)
fun <- function(t)
if (t) 0:1 else 0
expand.grid(lapply(as.list(exampleVec), fun))
Am 20.02.2012 14:15, schrieb jing tang:
Hi, I need some neat ways of determing a subset of binary strings. For example, x=c(0,0,1), y=c(0,1,1), z=c(0,1,0). So x is a subset of y and z is also a subset of y, but x is not a subset of z. I tried to search R functions and packages but no hits. Any ideas? Best, Jing -- Jing Tang, PhD Senior Researcher Finnish Institute of Molecular Medicine (FIMM) FI-00014 University of Helsinki Finland [[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.
Enrico Schumann Lucerne, Switzerland http://nmof.net/
On Mon, Feb 20, 2012 at 03:15:53PM +0200, jing tang wrote:
Hi, I need some neat ways of determing a subset of binary strings. For example, x=c(0,0,1), y=c(0,1,1), z=c(0,1,0). So x is a subset of y and z is also a subset of y, but x is not a subset of z. I tried to search R functions and packages but no hits. Any ideas?
Hi. Try this all(x <= y) # [1] TRUE all(z <= y) # [1] TRUE all(x <= z) # [1] FALSE Hope this helps. Petr Savicky.