I have a dataframe like this:
x | y | x1 | y1 | x2 | y2 | x3 | y3 |
---|---|---|---|---|---|---|---|
1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 | 2 | 0 |
1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
I want to find rows that x=x1 x2 x3 and rows that y=y1 y2 y3. Here is my code to check x=x1 x2 x3:
col_x = c(3,5,7)
df[df$x == rowSums(df[col_x])]
Suppose return row 1,3,4, but it returned
x x1 y1 x2 x3 y3
1 1 1 0 0 0 0
2 0 3 0 0 0 0
3 2 0 0 0 2 0
4 1 0 0 1 0 0
I also tried
col_x = c(3,5,7)
df[df$x == apply(df[col_x],1,sum)]
Which also give me:
x x1 y1 x2 x3 y3
1 1 1 0 0 0 0
2 0 3 0 0 0 0
3 2 0 0 0 2 0
4 1 0 0 1 0 0
I can't figure out why it returned all rows and it had skip column y2.
CodePudding user response:
You are just missing a comma.
col_x = c(3,5,7)
df[df$x == rowSums(df[col_x]),]
x y x1 y1 x2 y2 x3 y3
1 1 0 1 0 0 0 0 0
3 2 0 0 0 0 0 2 0
4 1 0 0 0 1 0 0 0
CodePudding user response:
A possible solution:
library(dplyr)
df %>%
filter(x == rowSums(across(matches("x\\d$"))) &
y == rowSums(across(matches("y\\d$"))))
#> x y x1 y1 x2 y2 x3 y3
#> 1 1 0 1 0 0 0 0 0
#> 2 2 0 0 0 0 0 2 0
#> 3 1 0 0 0 1 0 0 0