Display subsection between two dates in an R data frame

I am trying to extract a range of data from a dataframe between two specific dates in time; such as extracting the CFS (velocity) of a river between two dates. I've searched my stack from R books and all over the internet and found several possible answers, but didn't work for my specific application.

I would suggest that this should be a fairly simple task to achieve in R. I can accomplish this in my sleep using MySQl, but you need to accomplish it in R.

Data frame format:

Date, CFS, Temp,

2015-06-01 00:00, 2009, 12.5

2015-06-01 00:30, 2010, 12.5

data <- read.table("~/SomeObscureDataDirectory/RiverDataFull", sep="\t", header=TRUE) sub1 <- subset(data, data$Date >= 2015-06-01 00:00 & data$Date <= 2015-06-01 07:15)

// ---------------->

> data <- read.table("~/SomeObscureDataDirectory", sep="\t", header=TRUE)
> sub1 <- subset(data, data$Date >= 2015-06-01 00:00 & data$Date <=2015-06-01 07:15)
    Error: unexpected numeric constant in "sub1 <- subset(data, data$Date >= 2015-06-01 00"

      

I am at a loss for a solution, your expertise will be very grateful to you. I'm assuming the date format doesn't allow me to search against it? Note. I cannot change the format in the RiverDataFull.tab file it is in.

+3


source to share


1 answer


The column of date

your dataframe is a factor. Therefore, you will need to convert this column to a date type and map it to a different date type. You can use a function as.POSIXct()

for this:

data$Date <- as.POSIXct(data$Date, format='%Y/%m/%d %H:%M');    # convert to date
sub1 <- subset(data, data$Date >= as.POSIXct("2015-06-01 00:00") &
               data$Date <= as.POSIXct("2015-06-01 07:15"))

      



In the original code, you tried to use raw numbers and strings and it won't work.

+4


source







All Articles