user6678274 user6678274 - 1 year ago 73
R Question

How to make iterations over dates in R

This is a more general problem I get in R. Say I want to create a subset for a dataset

that contains the first 10 days the days 1,...,10. For a single day I can easy make the subset like this

data_new=subset(data, data$time == as.Date(as.character(2016-01-01)) )

But say I want the first 10 days in January 2016. I try to make a loop like this

for(j in 1:10) {
data_new[,j]= subset(data, data$time==as.Date(as.character(2016-01-j)))

but this code can not run in R because of the term

How can I create such a subset?

Answer Source

You could do

data_new = subset(data, data$time %in% as.Date(paste0("2016-01-", 1:10)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download