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!
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!
 
    
    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)
 
    
    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"))
 
    
    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)
}
