0

I am looking to loop over a vector of dates, using these dates as a subsetting criterion and carry out calculations. For simplicity's sake we will assume these calculations are a count of rows.

The problem is R treats the vector of dates as 5 digit numbers. This is despite having been coerced as dates using as.Date, therefore, loop creates a list of length 17,896. In my loop list there are only 12 dates.

I very much look forward to any suggestions. Thank you.

# first date of each month in 2018
dates_2018 = seq(as.Date("2018-1-1"), as.Date("2018-12-31"), "days")
loop_date = as.Date(as.vector(tapply(dates_2018, substr(dates_2018, 1, 7), max), mode="any"), origin = "1970-01-01")

# dummy df
df = data.frame(id = 1:length(dates_2018)
                ,dates_2018)

# count number of days satisfy criteria
y = list()
for (i in loop_date)
{
  y[[i]] = nrow(df[df$dates_2018 >= i, ])
}; y
Sweepy Dodo
  • 1,761
  • 9
  • 15

1 Answers1

1

You can do y[[i]] = nrow(df[df$dates_2018 >= as.Date(i,origin = "1970-01-01"), ]) and get a result by y[[17562]], but you will find your result in a list with 17,896 elments. Here is more proper

for (i in seq_along(loop_date))
{
  y[[i]] = nrow(df[df$dates_2018 >= loop_date[i], ])
}
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • Works! Thank you. I am guessing purpose of seq_along was to create an id for each element of loop_date by which the i in loop refers to rather than the element itself? – Sweepy Dodo Feb 15 '19 at 14:18
  • @T.Fung yes that is correct and much safer than using `1:length(x)`, see [here](https://stackoverflow.com/questions/41402294/using-seq-along-to-handle-the-empty-case) – A. Suliman Feb 15 '19 at 14:29