r - Subset a dataframe by time -



r - Subset a dataframe by time -

i working on dataframe have intergrated time , date 1 column (called timestamp):

a <-c(1:21) d <- c("2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14") time <- c("18:40:37", "18:40:48", "18:40:58", "18:41:08","18:41:18","18:41:28","18:41:38","18:41:48","18:41:58","18:42:08","18:42:18","18:42:28","18:42:38","18:42:48","18:42:58","18:43:08","18:43:18","18:42:28", "18:44:18", "18:44:28", "18:44:28") df1 <- data.frame(a, d, time) df1 <- within(df1, { timestamp=format(as.posixct(paste(d, time)), "%d/%m/%y %h:%m:%s") })

how subset dataframe exclude values after specific point in time? found code in stackoverflow similar question thought might able help, struggling time element work:

subset(df1, format.date(timestamp, ""%d/%m/%y %h:%m:%s"") >"14/12/2012 18:42:00")

any advice much appreciated.

edit: struggling code detailed below working on real data. dput() of first 4 rows of dataframe listed @ end of post. used line of code recommended @arun timestamp data.

gps <- within(gps, { timestamp=format(as.posixct(paste(local.date, local.time)), + "%d/%m/%y %h:%m:%s") })

if seek , apply sec part of code (strptime...) error message: error in $<-.data.frame(*tmp*, "timestamp", value = list(sec = c(37, : replacement has 30208 rows, info has 4 sort of explains when seek , appy code whole of info 8 rows of many numbers, separated comma. if can help me in way, extremely grateful.

structure(list(timestamp = c("14/12/2012 18:40:37", "14/12/2012 18:40:48", "14/12/2012 18:40:58", "14/12/2012 18:41:08"), latitude = c(54.77769505, 54.77765729, 54.77768751, 54.7777021), longitude = c(-1.56627049, -1.56639255, -1.56626555, -1.56662523), height = c(" 173.911 m", " 161.742 m", " 146.905 m", " 138.016 m"), speed = c(" 0.465 km/h", " 0.728 km/h", " 4.574 km/h", " 17.335 km/h")), .names = c("timestamp", "latitude", "longitude", "height", "speed"), row.names = c(na, 4l), class = "data.frame")

second edit: many @arun solution. bit confused how suppose utilize code info in date , time columns (local.date , local.time). used first line of code orginal solution, , sec line revised edits.

this code used:

gps <- within(gps, { timestamp=format(as.posixct(paste(local.date, local.time)), "%d/%m/%y %h:%m:%s") }) gps$timestamp <- strptime(gps$timestamp, "%y-%m-%d %h:%m:%s")

however string of nas (and -1s). apologies if used code in wrong way...

third edit apologies confusion @arun. when seek both ways round date column, errors. if maintain yr/m/d, how original info formatted, dput() of :

structure(list(timestamp = c("2012/12/14 18:40:37", "2012/12/14 18:40:48", "2012/12/14 18:40:58", "2012/12/14 18:41:08"), latitude = c(54.77769505, 54.77765729, 54.77768751, 54.7777021), longitude = c(-1.56627049, -1.56639255, -1.56626555, -1.56662523), height = c(" 173.911 m", " 161.742 m", " 146.905 m", " 138.016 m"), speed = c(" 0.465 km/h", " 0.728 km/h", " 4.574 km/h", " 17.335 km/h")), .names = c("timestamp", "latitude", "longitude", "height", "speed"), row.names = c(na, 4l), class = "data.frame")

if use:

gps2$timestamp <- strptime(gps2$timestamp, "%y/%m/%d %h:%m:%s")

... , seek view dataframe in r studio's workspace window, r session aborts.

its improve load character vectors such , not factors using stringsasfactors = false (as shown below)

# create sure character columns not converted factors df1 <- data.frame(a, d, time, stringsasfactors = false)

then,

df1 <- within(df1, { timestamp=format(as.posixct(paste(d, time)), "%d/%m/%y %h:%m:%s") }) # convert timestamp here df1$timestamp <- strptime(df1$timestamp, "%d/%m/%y %h:%m:%s")

now, seek subset way:

# subset subset(df1, timestamp > strptime("14/12/2012 18:42:00", "%d/%m/%y %h:%m:%s")) # d time timestamp # 10 10 2012/12/14 18:42:08 2012-12-14 18:42:08 # 11 11 2012/12/14 18:42:18 2012-12-14 18:42:18 # 12 12 2012/12/14 18:42:28 2012-12-14 18:42:28 # 13 13 2012/12/14 18:42:38 2012-12-14 18:42:38 # 14 14 2012/12/14 18:42:48 2012-12-14 18:42:48 # 15 15 2012/12/14 18:42:58 2012-12-14 18:42:58 # 16 16 2012/12/14 18:43:08 2012-12-14 18:43:08 # 17 17 2012/12/14 18:43:18 2012-12-14 18:43:18 # 18 18 2012/12/14 18:42:28 2012-12-14 18:42:28 # 19 19 2012/12/14 18:44:18 2012-12-14 18:44:18 # 20 20 2012/12/14 18:44:28 2012-12-14 18:44:28 # 21 21 2012/12/14 18:44:28 2012-12-14 18:44:28

edit: seek this:

df1 <- within(df1, { timestamp=as.posixct(timestamp, format = "%d/%m/%y %h:%m:%s") }) df1$timestamp <- strptime(df1$timestamp, "%y-%m-%d %h:%m:%s")

r

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -