[R] union of a list of logical values
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 NANA # 3 4.7 NA 1.3 NA setosa # 4 NA NA NA NANA # 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 FALSETRUE # 2FALSE FALSEFALSE FALSE FALSE # 3 TRUE FALSE TRUE FALSETRUE # 4FALSE FALSEFALSE FALSE FALSE # 5 TRUE FALSE TRUE FALSETRUE # 6 TRUE FALSE TRUE FALSETRUE 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 __ 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.
Re: [R] union of a list of logical values
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 NANA # 3 4.7 NA 1.3 NA setosa # 4 NA NA NA NANA # 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 FALSETRUE # 2FALSE FALSEFALSE FALSE FALSE # 3 TRUE FALSE TRUE FALSETRUE # 4FALSE FALSEFALSE FALSE FALSE # 5 TRUE FALSE TRUE FALSETRUE # 6 TRUE FALSE TRUE FALSETRUE 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, rip...@stats.ox.ac.uk 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, UKFax: +44 1865 272595 __ 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.
Re: [R] union of a list of logical values
On Mon, Jul 22, 2013 at 11:37 AM, Prof Brian Ripley rip...@stats.ox.ac.uk 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 NANA # 3 4.7 NA 1.3 NA setosa # 4 NA NA NA NANA # 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 FALSETRUE # 2FALSE FALSEFALSE FALSE FALSE # 3 TRUE FALSE TRUE FALSETRUE # 4FALSE FALSEFALSE FALSE FALSE # 5 TRUE FALSE TRUE FALSETRUE # 6 TRUE FALSE TRUE FALSETRUE 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, rip...@stats.ox.ac.uk 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, UKFax: +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 __ 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.