'Filter data frame using the last_col() function in dplyr R

I have multiple data frames that look this

time <- c(1,1,1,1,2,2,2,2,3,3,3,3)
ID <- c(1,2,3,4,1,2,3,4,1,2,3,4)
value <- c(0,0.1,0.2,0.4,0,0.05,0.05,0.5,0.20,0.40,0.50,0.60)

test <- data.frame(time, ID, value)
test

  time ID value
1     1  1  0.00
2     1  2  0.10
3     1  3  0.20
4     1  4  0.40
5     2  1  0.00
6     2  2  0.05
7     2  3  0.05
8     2  4  0.50
9     3  1  0.20
10    3  2  0.40
11    3  3  0.50
12    3  4  0.6

I would like to be able to filter the data frame based on the values that are smaller than 0.05 in the last column. I know I can use easily in baseR test[,ncol(test)] <0.05 is there a way that I can incorporate that in dplyr pipe or use that last_col() function something like: test %>% filter(.,last_col()<0.05)

Any help is appreciated



Solution 1:[1]

I use the following code to filter with dplyr and filter

library(dplyr)

time <- c(1,1,1,1,2,2,2,2,3,3,3,3)
ID <- c(1,2,3,4,1,2,3,4,1,2,3,4)
value <- c(0,0.1,0.2,0.4,0,0.05,0.05,0.5,0.20,0.40,0.50,0.60)

test <- data.frame(time, ID, value)

test.filter <- test %>%
                  filter(value <= 0.05)

Solution 2:[2]

This syntax figures out the variable name of the last column, and uses the basic filter

test %>% filter(!!sym((variable.names(test))[ncol(test)]) < 0.05)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OTStats
Solution 2 Cannon