-1

I have a data frame with a certain number of rows. Would like to drop all rows after a specific row number or after a date.

Any suggestions? Could not find anything on the web that works for me for the moment...

isabellko
  • 11
  • 1
  • 3
    Can you make this a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? Right now the best anyone can do is guess as to what your data looks like, how you're approaching this, what hasn't worked, the logic behind dropping after a row number vs after a date, etc. – camille Jan 04 '20 at 18:00

2 Answers2

3

Here's a way how you can do this:

df <- df[1:2, ] ## one way of selecting rows from first row to row number you want in a data frame
#  a b c       date
#1 1 2 3 2017-01-01
#2 1 2 3 2017-01-02

df <- df[-(3:nrow(df)), ] ## another way of filtering rows from starting from row which you don't want to total number of rows in a data frame
#  a b c       date
#1 1 2 3 2017-01-01
#2 1 2 3 2017-01-02

df <- df[df$date < "2017-01-03", ] ## subset based on a date value
#  a b c       date
#1 1 2 3 2017-01-01
#2 1 2 3 2017-01-02

data

df = data.frame(a = c(1,1,4,4), b = c(2,2,5,5), c = c(3,3,6,6), 
                date = seq(from = as.Date("2017-01-01"), to = as.Date("2017-01-04"), by = 'day')) ## creating a dummy data frame
sm925
  • 2,648
  • 1
  • 16
  • 28
1

We can use head

n <- 5
df2 <- head(df1, n)
df2
#        date       col2
#1 2019-01-01 -0.5458808
#2 2019-02-01  0.5365853
#3 2019-03-01  0.4196231
#4 2019-04-01 -0.5836272
#5 2019-05-01  0.8474600

Or create a logical vector

df1[seq_len(nrow(df1)) <= n, ]

Or another option is slice

library(dplyr)
df1 %>%
   slice(seq_len(n))

Or with data.table

library(data.table)
setDT(df1)[seq_len(n)]

If it is based on a date value

date1 <- as.Date("2019-05-01")
subset(df1, date <= date1)

data

set.seed(24)
df1 <- data.frame(date = seq(as.Date("2019-01-01"), length.out = 10, 
       by = "month"), col2 = rnorm(10))
akrun
  • 874,273
  • 37
  • 540
  • 662