Reputation: 479
I have a dataset made of 3 columns and for each column, the values will vary between -1 and 1.
A B C
0.0373 0.0373 0.0373
-0.0586 -0.0407 -0.1109
0.0458 0.0458 0.0458
0.0621 0.0621 0.0621
0.0452 0.0452 0.0452
I wish to identify all rows for which the sign for A, B and C is not the same. So for example, rows 2, 4 and 5 should be returned, because the sign of one of the column values is not the same as the other two.
I started out with this solution but was wondering if there might be something simpler and cleaner.
df$test = (df$A > 0 & df$B > 0 & df$C > 0)
df$test2 = (df$A < 0 & df$B < 0 & df$C < 0)
result = subset(df,df$test == FALSE & df$test2 == FALSE)
A B C test test2
-0.0586 -0.0407 0.1109 FALSE FALSE
0.0621 -0.0621 0.0621 FALSE FALSE
-0.0452 0.0452 0.0452 FALSE FALSE
Thanks.
Upvotes: 4
Views: 492
Reputation: 887391
We can use rowSums
i1 <- (!rowSums(df > 0)) & (!rowSums(df < 0))
df[i1,]
Upvotes: 5