datetime - How do I subset data based on a date range in r? -
i have large .txt data file , need subset based on date range.
head(newfile) date time global_active_power global_reactive_power voltage global_intensity 1 16/12/2006 17:24:00 4.216 0.418 234.84 18.4 2 16/12/2006 17:25:00 5.360 0.436 233.63 23.0 3 16/12/2006 17:26:00 5.374 0.498 233.29 23.0 4 16/12/2006 17:27:00 5.388 0.502 233.74 23.0 5 16/12/2006 17:28:00 3.666 0.528 235.68 15.8 6 16/12/2006 17:29:00 3.520 0.522 235.02 15.0 sub_metering_1 sub_metering_2 sub_metering_3 1 0 1 17 2 0 1 16 3 0 2 17 4 0 1 17 5 0 1 17 6 0 2 17
i need use data dates 2007-02-01 , 2007-02-02.
i think need convert date , time variables date/time classes in r using strptime()
, as.date()
functions, i'm not clear on how that.
what simplest/cleanest way this?
you can use lubridate library, code example, make little change data
library(lubridate) > df <- read.table("test2.txt", header=true) > df date time global_active_power global_reactive_power voltage 1 16/12/2006 17:24:00 4.216 0.418 234.84 2 16/12/2006 17:25:00 5.360 0.436 233.63 3 16/12/2007 17:26:00 5.374 0.498 233.29 4 16/12/2007 17:27:00 5.388 0.502 233.74 5 16/12/2006 17:28:00 3.666 0.528 235.68 global_intensity 1 18.4 2 23.0 3 23.0 4 23.0 5 15.8 > date1 = dmy("04/06/2007") > date2 = dmy("04/06/2009") > with( df , df[ dmy(df$date) >= date1 ,dmy(df$date) <= date2 ] ) date time global_active_power global_reactive_power voltage 3 16/12/2007 17:26:00 5.374 0.498 233.29 4 16/12/2007 17:27:00 5.388 0.502 233.74 global_intensity 3 23 4 23 >
Comments
Post a Comment