I wonder how can we filter rows with the same values in columns using dplyr
package? I tried doing something in opposite to what was asked and proposed in this question but nothing worked.
I used the approach with apply
function but received the following error:
same_vals <- apply(mydata, 1, function(x) all(x == x[1]))
final <- mydata %>%
filter(same_vals)
Error: Can't subset elements that don't exist.
x Location 1 doesn't exist.
i There are only 0 elements.
apply
drives me crazy every time I try to use it. Neither it worked on my sample data:
set.seed(2022)
test <- tibble(id = floor(runif(10, min = 0, max = 111)),
var1 = ceiling(runif(10, min = 5, max = 10)),
var2 = c(6, 5, 4, 8, 12, 1223, 14, 1, 90, 1),
var3 = c(6, 3, 4, 8, 11, 45, 56, 78, 0, 9))
# A tibble: 10 x 4
id var1 var2 var3
<dbl> <dbl> <dbl> <dbl>
1 90 6 6 6
2 71 6 5 3
3 13 6 4 4
4 60 8 8 8
5 20 9 12 11
6 70 6 1223 45
7 8 9 14 56
8 4 8 1 78
9 41 8 90 0
10 84 10 1 9
test1 <- apply(test, 1, function(x) all(x == x[1]))
test %>%
filter(test1)
# A tibble: 0 x 4
# ... with 4 variables: id <dbl>, var1 <dbl>, var2 <dbl>, var3 <dbl>
Desirable output
# A tibble: 10 x 4
id var1 var2 var3
<dbl> <dbl> <dbl> <dbl>
1 90 6 6 6
4 60 8 8 8