Skip to content

Instantly share code, notes, and snippets.

@stevehenderson
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevehenderson/9695799 to your computer and use it in GitHub Desktop.
Save stevehenderson/9695799 to your computer and use it in GitHub Desktop.
Split R Dataframe by Date #split #dataframe #R #date
df <- data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-01 11:23"), by="day", length.out=4), each=4), var=rnorm(4))
df <- rbind(df,data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-02 01:20"), by="day", length.out=4), each=4), var=rnorm(4)))
df <- rbind(df,data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-02 05:13"), by="day", length.out=4), each=4), var=rnorm(4)))
df <- rbind(df,data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-03 18:22"), by="day", length.out=4), each=4), var=rnorm(4)))
df <- rbind(df,data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-11 16:44"), by="day", length.out=4), each=4), var=rnorm(4)))
df <- rbind(df, data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-11 02:26"), by="day", length.out=4), each=4), var=rnorm(4)))
df <- rbind(df,data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-12 13:13"), by="day", length.out=4), each=4), var=rnorm(4)))
df <- rbind(df,data.frame(date=rep(seq.POSIXt(as.POSIXct("2011-11-13 19:33"), by="day", length.out=4), each=4), var=rnorm(4)))
split(df, cut(strptime(paste(df$date), format="%F"),"7 day"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment