Filter/Subset unique row id

Viewed 50

I would like to use R's unique rowid value to filter a dataset. Although my dataset is quite large, for ease of posting this query I've conceptualized the data table and the query that I'd like to run. I'm getting an error (Caused by error in row.names > 3) when I tried running the code. Any help appreciated.

temp <- data.frame(Text = c("a","b","c","d","e"))
a <- temp %>% 
  filter(row.names > 3)

Expected output
    Text
4    d
5    e
3 Answers

Try this and see if its whats your looking for:

a <- temp %>% 
  mutate(row.number = row_number()) %>%
  filter(row.number > 3)

Maybe doing this:

temp[4:nrow(temp),, drop = FALSE]

or:

temp %>% slice(4:nrow(temp))

It's not advisable to work with row names.

1) Use slice_tail. n = -3 means all but the first 3 rows.

library(dplyr)

temp %>%
  slice_tail(n = -3)
##   Text
## 1    d
## 2    e

2) Alternately using only base R:

tail(temp, -3)
##   Text
## 4    d
## 5    e
Related