3

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

LDT
  • 2,856
  • 2
  • 15
  • 32

3 Answers3

7

dplyr >= 1.0.0

Using dplyr::across:

df %>% 
  dplyr::filter(across(last_col(), ~ . < 0.05))

across works here since last_col returns a single column. If you're working with more than one column I suggest using if_any or if_all (depending on your logic). For more information see this SO answer.


dplyr < 1.0.0

Using filter_at:

df %>%
  dplyr::filter_at(vars(last_col()), ~ . < 0.05)

vars and last_col are also from the dplyr package.

LMc
  • 12,577
  • 3
  • 31
  • 43
2

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)
OTStats
  • 1,820
  • 1
  • 13
  • 22
andres_995
  • 141
  • 1
  • 5
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)
Cannon
  • 67
  • 6