Reputation: 866
I have a data set in which I want to compare two columns. For each row where these column don't match I want to capture the corresponding row value of another value and write it in another data frame.
Input Data
data <- structure(list(A = 1:7, B = c(1L, 2L, 1L, 1L, 1L, 9L, 9L), C = c(1L,
3L, 2L, 1L, 6L, 8L, 1L)), class = "data.frame", row.names = c(NA,
-7L))
Expected Output
A
1 2
2 3
3 5
4 6
5 7
Currently I am using following code but this leads to empty rows when the value for B
and C
column matches in Input Data. I want to keep those of A
for which B
and C
don't match.
Is it possible to it any another way so ensure above condition is met?
Code
ifelse(data$B == data$C,
data$A, "")
Output of Above Code
[1] "" "2" "3" "" "5" "6" "7"
Upvotes: 1
Views: 288
Reputation: 887153
We can just use subset
subset(data, B != C, select = A)
# A
#2 2
#3 3
#5 5
#6 6
#7 7
Or with filter
library(dplyr)
data %>%
filter(B != C) %>%
select(A)
Or using data.table
library(data.table)
setDT(data)[B != C, .(A)]
Upvotes: 1
Reputation: 388982
You could select A
values where B != C
and then add it to a new data frame.
data.frame(A = data$A[data$B != data$C])
# A
#1 2
#2 3
#3 5
#4 6
#5 7
If you just need vector of values you could do
data$A[data$B != data$C]
#[1] 2 3 5 6 7
In ifelse
we are replacing the values which do not satisfy the condition with empty space (""
) hence the length of output is same as number of rows in data
.
Upvotes: 1