Reputation: 1085
Here's some dummy code and its pretty obvious output:
xdupe <- as.logical(c("T", "F", "F", "F", "T", "T", "F"))
ydupe <- as.logical(c("T", "F", "F", "F", "F", "T", "T"))
cities <- c("Knox", "Whiteville", "Madison", "York", "Paris", "Corona", "Bakersfield")
df <- data.frame(cities, xdupe, ydupe)
df$cities <- as.character(df$cities)
> df
cities xdupe ydupe
1 Knox TRUE TRUE
2 Whiteville FALSE FALSE
3 Madison FALSE FALSE
4 York FALSE FALSE
5 Paris TRUE FALSE
6 Corona TRUE TRUE
7 Bakersfield FALSE TRUE
For some context, what xdupe and ydupe represent are logical values for duplicated x and y coordinates (longitude and latitude, respectively).
So what I need to be able to do is see which attributes in the data frame have TRUE values for both xdupe and ydupe. In this particular case, that would be the cities Knox and Corona. How can I compare xdupe and y dupe so that I can pull out all of the cities that have BOTH true values?
Upvotes: 0
Views: 1984
Reputation: 887153
This can be done in a couple of ways. One option is &
. It will become TRUE
only when all the elements are TRUE
. So, if we use xdupe & ydupe
, it will compare the corresponding elements of 'xdupe' and 'ydupe' and get TRUE
only when both elements are 'TRUE'.
i1 <- with(df, xdupe & ydupe)
Or another option is rowSums
of the subset of dataset that includes only the logical columns. As the binary representation for TRUE is 1 and FALSE is 0, when there are both TRUE for each corresponding element, it will result in 2.
i1 <- rowSums(df[-1])==2
and then we subset the dataset
Subdf <- df[i1,]
Subdf
# cities xdupe ydupe
#1 Knox TRUE TRUE
#6 Corona TRUE TRUE
Upvotes: 3