r - filtering data.frame based on row_number() -


update: dplyr has been updated since question asked , performs op wanted

i´m trying second seventh line in data.frame using dplyr.

i´m doing this:

require(dplyr) df <- data.frame(id = 1:10, var = runif(10)) df <- df %>% filter(row_number() <= 7, row_number() >= 2) 

but throws error.

error in rank(x, ties.method = "first") :    argument "x" missing, no default 

i know make:

df <- df %>% mutate(rn = row_number()) %>% filter(rn <= 7, rn >= 2) 

but understand why first try not working.

the row_number() function not return row number of each element , can't used want:

• ‘row_number’: equivalent ‘rank(ties.method = "first")’

you're not saying want row_number of. in case:

df %>% filter(row_number(id) <= 7, row_number(id) >= 2) 

works because id sorted , row_number(id) 1:10. don't know row_number() evaluates in context, when called second time dplyr has run out of things feed , equivalent of:

> row_number() error in rank(x, ties.method = "first") :    argument "x" missing, no default 

that's error right there.

anyway, that's not way select rows.

you need subscript df[2:7,], or if insist on pipes everywhere:

> df %>% "["(.,2:7,)   id        var 2  2 0.52352994 3  3 0.02994982 4  4 0.90074801 5  5 0.68935493 6  6 0.57012344 7  7 0.01489950 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -