1

I am looking for a code that is giving me just the rows that contain the first day of each month

 (`1999-01-01`, `1999-02-01`, ..., `2010-11-01`, `202010-12-01`).

Does anyone have an idea how to solve this?

I would really appreciate your help!

bli12blu12
  • 357
  • 2
  • 11
  • 3
    you can accomplish this with the `seq.Date()` function, like so: `seq.Date(as.Date("2008-01-01"), as.Date("2017-12-01"), by = "month")` – 93i7hdjb May 02 '18 at 18:37
  • 4
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data aren't particularly helpful. – MrFlick May 02 '18 at 18:41

3 Answers3

1

Assuming your date column is properly formatted as a date object, you can subset your data frame with a condition that the date column falls on the first of the month, like so:

dates_i_want <- seq.Date(as.Date("2008-01-01"), as.Date("2017-12-01"), by = "month")
result <- subset(data, date %in% dates_i_want)
93i7hdjb
  • 1,136
  • 1
  • 9
  • 15
0

You can use indexing, making a logical index to show which entries in the dataframe are the first of the month. Use seq.Date to create an index:

# create index
ind <- seq.Date(as.Date("2008-01-01"), as.Date("2017-12-01"), by = "month")
# index the dataframe 
df2 <- df[df[, 1] %in% ind, ]

This is taking the rows where the entries in df are found in the ind vector. For what it's worth, this approach is marginally (15%) faster than the subset approach.


# dummy data 
df <- data.frame("x" = seq.Date(as.Date("2008-01-01"), as.Date("2017-12-01"), by = "day"))
rg255
  • 4,119
  • 3
  • 22
  • 40
0

Building on response from Erik Kornet, you can create a sequence and then use it to filter for just the rows you want. Assuming a dataframe named x with a variable of date:

myseq <- seq(as.Date("2008-01-01"), as.Date("2017-12-01"), by= "month")
for(k in 1:length(myseq)){
  temp <- (x[x$date==myseq[k],])
  x3 <- rbind(x3, temp)
}
mvinton
  • 1
  • 1
  • This is not improving Erik's solution at all in terms of readability and will also be much slower. – s_baldur May 03 '18 at 09:20
  • 1
    First rule of R: *Never grow a vector*. – s_baldur May 03 '18 at 09:21
  • Snoram, I wrote this in response to his comment which only provided a sequence so it is an addition to that first comment he made. Agree it will be slower than his posted solution. No idea where you got your first rule, makes no sense. Thanks for your kindness. – mvinton May 04 '18 at 10:30
  • It is not exactly a rule but considered good practice if avoidable. Maybe it doesn't make sense to *you* yet but you could have look here: https://books.google.is/books?id=8kWvDQAAQBAJ&pg=PA49&lpg=PA49&dq=rule+number+1+R+never+grow+object+vector&source=bl&ots=Aaum1ZJZDh&sig=YAT7Xr0JTNyQgRhP3vmJkxmlrXI&hl=is&sa=X&ved=0ahUKEwjZ9piB-OvaAhWKyqQKHQZdBiAQ6AEIPDAG#v=onepage&q=rule%20number%201%20R%20never%20grow%20object%20vector&f=false – s_baldur May 04 '18 at 11:22