Reputation: 23
Good morning,
I am coding in R. I have three logical vectors :
1 2 3 4 5 6
a T T F F T F
b F T F F F F
c F F F T F F
And I would like to obtain a vector telling me if a position is set to true in at least one of the three vector :
1 2 3 4 5 6
a T T F T T F
I have tried to used :
Reduce("&&",a,b,c)
But it didn't work.
Thank you if you have any idea or advice to solve my problem,
Upvotes: 2
Views: 3910
Reputation: 79208
since your data is in a matrix format, you can do:
colSums(dat)>0
1 2 3 4 5 6
TRUE TRUE FALSE TRUE TRUE FALSE
data:
dat=read.table(text=" 1 2 3 4 5 6
a T T F F T F
b F T F F F F
c F F F T F F",strip=T,h=T)
names(dat)=1:6
Upvotes: 2
Reputation: 11128
May be this can do as well:
Reduce(`+`,list(a,b,c)) > 0
or more simply could be:
a+b+c > 0
Where Input can be:
a <- c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE)
b <- c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)
c <- c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)
Output:
> Reduce(`+`,list(a,b,c)) > 0
[1] TRUE TRUE FALSE TRUE TRUE FALSE
Upvotes: 1
Reputation: 887078
We can use |
in this case
a|b|c
If there are multiple vectors, place it in a list
and use Reduce
with |
Reduce(`|`, list(a, b, c))
a <- c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE)
b <- c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)
c <- c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)
Upvotes: 5