Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr filter by the first column

Tags:

r

filter

dplyr

Is it possible to filter in dplyr by the position of a column?

I know how to do it without dplyr

iris[iris[,1]>6,]

But how can I do it in dplyr?

Thanks!

like image 772
lokheart Avatar asked Nov 28 '22 22:11

lokheart


2 Answers

Besides the suggestion by @thelatemail, you can also use filter_at and pass the column number to vars parameter:

iris %>% filter_at(1, all_vars(. > 6))

all(iris %>% filter_at(1, all_vars(. > 6)) == iris[iris[,1] > 6, ])
# [1] TRUE
like image 81
Psidom Avatar answered Dec 01 '22 10:12

Psidom


No magic, just use the item column number as per above, rather than the variable (column) name:

library("dplyr")

iris %>%
  filter(iris[,1] > 6)

Which as @eipi10 commented is better as

iris %>%
  filter(.[[1]] > 6)
like image 39
Scransom Avatar answered Dec 01 '22 11:12

Scransom