Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first date from a vector?

Tags:

r

posixct

I have a vector of POSIXct objects, I would like to determine the first and the last date/time value in the list.

POSIXct_vector <- read.csv(file="data", as.is=TRUE)
POSIXct_vector$DateTime <- as.POSIXct(POSIXct_vector)

#returns NA
min(POSIXct_vector$DateTime)

#returns NA
max(POSIXct_vector$DateTime)
like image 779
klonq Avatar asked Aug 19 '11 12:08

klonq


1 Answers

I suspect you need to add the na.rm=TRUE argument to your commands. This also means that at least one of the elements of your vector hasn't been resolved to a valid time. You can also use range to give the limits in one command.

dat <- as.POSIXct(rnorm(10,sd=1e6),origin=Sys.Date())

range(dat)
[1] "2011-07-25 12:36:23 BST" "2011-09-11 20:02:20 BST"

dat[3] <- NA
range(dat)
[1] NA NA
range(dat,na.rm=TRUE)
[1] "2011-08-02 06:42:05 BST" "2011-09-11 20:02:20 BST"
like image 61
James Avatar answered Nov 04 '22 15:11

James