union of a list of logical values

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

union of a list of logical values

Liviu Andronic
Dear all,
How can I obtain the union of a list of logical values?

Consider the following:
x <- head(iris)
x[,c(2,4)] <- NA
x[c(2,4),] <- NA
# > x
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1          NA          1.4          NA  setosa
# 2           NA          NA           NA          NA    <NA>
# 3          4.7          NA          1.3          NA  setosa
# 4           NA          NA           NA          NA    <NA>
# 5          5.0          NA          1.4          NA  setosa
# 6          5.4          NA          1.7          NA  setosa
z <- data.frame(!is.na(x))
# > z
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1         TRUE       FALSE         TRUE       FALSE    TRUE
# 2        FALSE       FALSE        FALSE       FALSE   FALSE
# 3         TRUE       FALSE         TRUE       FALSE    TRUE
# 4        FALSE       FALSE        FALSE       FALSE   FALSE
# 5         TRUE       FALSE         TRUE       FALSE    TRUE
# 6         TRUE       FALSE         TRUE       FALSE    TRUE

I did find a solution, but it seems more like a hack:
> ##union of logical values by rows (union of list of logical values)
> as.logical(rowSums(z))
[1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE
> ##union of logical values by columns
> as.logical(colSums(z))
[1]  TRUE FALSE  TRUE FALSE  TRUE

Another unusable monstrosity is as follows:
> ##union of list of logical values
> z[[1]] | z[[2]] | z[[3]] | z[[4]] | z[[5]]
[1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE

Is there a more elegant way to approach this problem and obtain the
above logical vectors? Regards,
Liviu


--
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: union of a list of logical values

Prof Brian Ripley
On 22/07/2013 10:16, Liviu Andronic wrote:
> Dear all,
> How can I obtain the union of a list of logical values?

This really only makes sense for a list of logical vectors of the same
length.  And by 'union' you seem to mean 'or'.

Two approaches

1) Make a logical matrix and use apply(m, 1, any)

2) Use Reduce(`|`, z)

>
> Consider the following:
> x <- head(iris)
> x[,c(2,4)] <- NA
> x[c(2,4),] <- NA
> # > x
> # Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> # 1          5.1          NA          1.4          NA  setosa
> # 2           NA          NA           NA          NA    <NA>
> # 3          4.7          NA          1.3          NA  setosa
> # 4           NA          NA           NA          NA    <NA>
> # 5          5.0          NA          1.4          NA  setosa
> # 6          5.4          NA          1.7          NA  setosa
> z <- data.frame(!is.na(x))
> # > z
> # Sepal.Length Sepal.Width Petal.Length Petal.Width Species
> # 1         TRUE       FALSE         TRUE       FALSE    TRUE
> # 2        FALSE       FALSE        FALSE       FALSE   FALSE
> # 3         TRUE       FALSE         TRUE       FALSE    TRUE
> # 4        FALSE       FALSE        FALSE       FALSE   FALSE
> # 5         TRUE       FALSE         TRUE       FALSE    TRUE
> # 6         TRUE       FALSE         TRUE       FALSE    TRUE
>
> I did find a solution, but it seems more like a hack:
>> ##union of logical values by rows (union of list of logical values)
>> as.logical(rowSums(z))
> [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE
>> ##union of logical values by columns
>> as.logical(colSums(z))
> [1]  TRUE FALSE  TRUE FALSE  TRUE
>
> Another unusable monstrosity is as follows:
>> ##union of list of logical values
>> z[[1]] | z[[2]] | z[[3]] | z[[4]] | z[[5]]
> [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE
>
> Is there a more elegant way to approach this problem and obtain the
> above logical vectors? Regards,
> Liviu
>
>


--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: union of a list of logical values

Liviu Andronic
On Mon, Jul 22, 2013 at 11:37 AM, Prof Brian Ripley
<[hidden email]> wrote:
> This really only makes sense for a list of logical vectors of the same
> length.  And by 'union' you seem to mean 'or'.
>
Indeed.


> Two approaches
>
> 1) Make a logical matrix and use apply(m, 1, any)
>
Of course! I tried apply(m, 1, "|") without luck, but I should have
used apply(m, 1, any) instead.

Thank you,
Liviu


> 2) Use Reduce(`|`, z)
>
>
>>
>> Consider the following:
>> x <- head(iris)
>> x[,c(2,4)] <- NA
>> x[c(2,4),] <- NA
>> # > x
>> # Sepal.Length Sepal.Width Petal.Length Petal.Width Species
>> # 1          5.1          NA          1.4          NA  setosa
>> # 2           NA          NA           NA          NA    <NA>
>> # 3          4.7          NA          1.3          NA  setosa
>> # 4           NA          NA           NA          NA    <NA>
>> # 5          5.0          NA          1.4          NA  setosa
>> # 6          5.4          NA          1.7          NA  setosa
>> z <- data.frame(!is.na(x))
>> # > z
>> # Sepal.Length Sepal.Width Petal.Length Petal.Width Species
>> # 1         TRUE       FALSE         TRUE       FALSE    TRUE
>> # 2        FALSE       FALSE        FALSE       FALSE   FALSE
>> # 3         TRUE       FALSE         TRUE       FALSE    TRUE
>> # 4        FALSE       FALSE        FALSE       FALSE   FALSE
>> # 5         TRUE       FALSE         TRUE       FALSE    TRUE
>> # 6         TRUE       FALSE         TRUE       FALSE    TRUE
>>
>> I did find a solution, but it seems more like a hack:
>>>
>>> ##union of logical values by rows (union of list of logical values)
>>> as.logical(rowSums(z))
>>
>> [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE
>>>
>>> ##union of logical values by columns
>>> as.logical(colSums(z))
>>
>> [1]  TRUE FALSE  TRUE FALSE  TRUE
>>
>> Another unusable monstrosity is as follows:
>>>
>>> ##union of list of logical values
>>> z[[1]] | z[[2]] | z[[3]] | z[[4]] | z[[5]]
>>
>> [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE
>>
>> Is there a more elegant way to approach this problem and obtain the
>> above logical vectors? Regards,
>> Liviu
>>
>>
>
>
> --
> Brian D. Ripley,                  [hidden email]
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595



--
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail

______________________________________________
[hidden email] 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.