[R] read.zoo problem reading in date time
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Mar 3 18:01:28 CET 2008
One must specify that there is a header if there is one, each % item
should only
be listed once, and format is currently only used for "Date" class so
you want to
use the FUN= argument -- although in the development version of zoo format is
availailable more generally. Here are several ways to do this. z1 is
created using
chron, z2 uses POSIXct and the z3 uses POSIXct and the new version of read.zoo
in the devel version of zoo.
library(zoo)
Lines <- "DateTime,Temp,SpCond,DOConc,Depth,pH,ORP,Turbidity+,Chlorophyll,Battery,Cond,DO%,Salinity,TDS
01/13/2006 17:01,10.87,84,9.36,0.664,7.3,132,28.8,3.1,11.5,0.062,84.6,0.04,0.055
01/13/2006 17:16,10.9,84,9.36,0.66,7.31,133,28.7,2.9,11.5,0.062,84.7,0.04,0.055
01/13/2006 17:31,10.92,84,9.36,0.655,7.3,132,28.4,2.6,11.4,0.062,84.8,0.04,0.055
01/13/2006 17:46,10.96,85,9.37,0.651,7.3,133,27.5,2.3,11.4,0.062,84.9,0.04,0.055
01/13/2006 18:01,10.96,85,9.35,0.644,7.33,132,28.2,2.9,11.4,0.062,84.7,0.04,0.055
01/13/2006 18:16,10.98,85,9.35,0.638,7.33,132,27.9,2.5,11.4,0.062,84.8,0.04,0.055
01/13/2006 18:31,11.01,85,9.35,0.635,7.31,131,26.4,3,11.5,0.062,84.8,0.04,0.055
01/13/2006 18:46,11.04,85,9.33,0.635,7.31,133,26.1,2.9,11.5,0.062,84.7,0.04,0.055
01/13/2006 19:01,11.04,85,9.33,0.632,7.33,133,26.3,2.9,11.4,0.062,84.7,0.04,0.055
01/13/2006 19:16,11.06,85,9.32,0.639,7.34,134,26,2.6,11.5,0.062,84.6,0.04,0.055
01/13/2006 19:31,11.11,85,9.37,0.656,7.35,136,25.6,2.1,11.4,0.062,85.2,0.04,0.055
01/13/2006 19:46,11.11,84,9.36,0.65,7.33,137,26.6,2.4,11.4,0.062,85.2,0.04,0.055
01/13/2006 20:01,11.18,84,9.41,0.649,7.35,138,27,2.8,11.4,0.062,85.7,0.04,0.055
01/13/2006 20:16,11.13,84,9.35,0.651,7.34,140,27.1,2.7,11.4,0.062,85,0.04,0.055
01/13/2006 20:31,11.12,84,9.33,0.656,7.32,142,26.7,2.9,11.4,0.062,84.8,0.04,0.055
01/13/2006 20:46,11.07,84,9.33,0.667,7.32,143,26.4,3.1,11.4,0.062,84.8,0.04,0.055
01/13/2006 21:01,11.05,84,9.35,0.67,7.31,145,26.6,2.7,11.4,0.062,84.9,0.04,0.055
01/13/2006 21:16,11.02,84,9.34,0.67,7.32,145,25.9,2.6,11.4,0.062,84.8,0.04,0.055
01/13/2006 21:31,11.01,84,9.35,0.673,7.32,146,26.3,3.1,11.4,0.062,84.8,0.04,0.055
01/13/2006 21:46,10.99,84,9.35,0.676,7.32,146,26.4,3.3,11.4,0.062,84.8,0.04,0.055
01/13/2006 22:01,10.96,84,9.34,0.673,7.32,146,26,2.6,11.5,0.062,84.7,0.04,0.055
01/13/2006 22:16,10.94,84,9.32,0.668,7.32,147,26.5,2.9,11.5,0.061,84.4,0.04,0.055
01/13/2006 22:31,10.93,84,9.3,0.666,7.31,148,25.8,2.4,11.4,0.062,84.3,0.04,0.055
01/13/2006 22:46,10.92,84,9.3,0.666,7.32,148,26,2.2,11.4,0.061,84.2,0.04,0.055
01/13/2006 23:01,10.91,84,9.27,0.666,7.32,148,26.5,2.9,11.5,0.062,83.9,0.04,0.055
01/13/2006 23:16,10.92,84,9.34,0.666,7.31,149,26.9,2.5,11.4,0.062,84.6,0.04,0.055
01/13/2006 23:31,10.93,84,9.31,0.667,7.33,148,25.8,2.8,11.4,0.062,84.3,0.04,0.055
01/13/2006 23:46,10.92,84,9.34,0.666,7.31,150,26.1,3.1,11.4,0.062,84.6,0.04,0.055
01/14/2006 00:01,10.95,84,9.28,0.667,7.3,144,26.4,2.5,11.5,0.062,84.1,0.04,0.055
01/14/2006 00:16,10.95,84,9.29,0.667,7.31,142,25.6,2.3,11.5,0.062,84.2,0.04,0.055
01/14/2006 00:31,10.94,84,9.29,0.669,7.3,142,25.6,2.4,11.4,0.062,84.2,0.04,0.055
01/14/2006 00:46,10.94,85,9.29,0.671,7.31,139,25.3,3.1,11.4,0.062,84.1,0.04,0.055
01/14/2006 01:01,10.95,85,9.26,0.672,7.3,141,25.3,2.6,11.3,0.062,83.9,0.04,0.055
01/14/2006 01:16,10.95,85,9.28,0.673,7.31,141,26,3.1,11.4,0.062,84,0.04,0.055
01/14/2006 01:31,10.96,85,9.27,0.673,7.34,139,25.4,2.1,11.4,0.062,84,0.04,0.055
01/14/2006 01:46,10.95,85,9.28,0.673,7.32,138,24.8,2.5,11.4,0.062,84.1,0.04,0.055
01/14/2006 02:01,10.93,85,9.27,0.673,7.31,137,25.1,2.2,11.4,0.062,84,0.04,0.055
01/14/2006 02:16,10.93,85,9.28,0.673,7.31,137,24.6,2,11.4,0.062,84.1,0.04,0.055
01/14/2006 02:31,10.94,85,9.29,0.671,7.35,133,24.8,2.8,11.4,0.062,84.2,0.04,0.055
01/14/2006 02:46,10.91,85,9.27,0.671,7.35,130,24.7,2.1,11.4,0.062,83.8,0.04,0.055
01/14/2006 03:01,10.92,85,9.28,0.67,7.32,131,24.5,2.6,11.4,0.062,84,0.04,0.055
01/14/2006 03:16,10.91,85,9.26,0.669,7.32,129,24.8,2.6,11.3,0.062,83.8,0.04,0.055
01/14/2006 03:31,10.9,85,9.28,0.671,7.32,129,24.5,2.5,11.4,0.062,84,0.04,0.055
01/14/2006 03:46,10.86,85,9.28,0.669,7.33,127,23.8,2.5,11.4,0.062,83.9,0.04,0.055
01/14/2006 04:01,10.87,85,9.27,0.669,7.33,128,24.3,2.7,11.4,0.062,83.9,0.04,0.055
01/14/2006 04:16,10.87,85,9.26,0.668,7.36,125,23.6,2.9,11.3,0.062,83.7,0.04,0.055
01/14/2006 04:31,10.8,85,9.28,0.667,7.34,123,23.6,2,11.3,0.062,83.8,0.04,0.056
01/14/2006 04:46,10.82,85,9.28,0.666,7.34,125,23.8,2.5,11.4,0.062,83.8,0.04,0.056
01/14/2006 05:01,10.82,85,9.26,0.665,7.33,124,24.3,2.3,11.3,0.062,83.7,0.04,0.056
01/14/2006 05:16,10.8,86,9.27,0.665,7.33,122,23.8,2.4,11.4,0.062,83.7,0.04,0.056
01/14/2006 05:31,10.78,86,9.28,0.667,7.35,122,24.1,3.1,11.3,0.062,83.7,0.04,0.056
01/14/2006 05:46,10.76,86,9.27,0.668,7.35,123,23.8,2.8,11.3,0.062,83.6,0.04,0.056
01/14/2006 06:01,10.77,86,9.26,0.668,7.34,116,24.2,2.6,11.3,0.062,83.5,0.04,0.056
"
# chron
library(chron)
fmt.chron <- function(x) {
chron(sub(" .*", "", x), gsub(".* (.*)", "\\1:00", x))
}
z1 <- read.zoo(textConnection(Lines), sep = ",", header = TRUE, FUN = fmt.chron)
# POSIXct
fmt.POSIXct <- function(x) {
as.POSIXct(strptime(x, "%m/%d/%Y %H:%M"))
}
z2 <- read.zoo(textConnection(Lines), sep = ",", header = TRUE, FUN =
fmt.POSIXct)
# POSIXct - using development version of zoo
# next line reads in devel version of read.zoo from R-Forge
source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/R/read.zoo.R?rev=445&root=zoo")
z3 <- read.zoo(textConnection(Lines), sep = ",", header = TRUE,
format = "%m/%d/%Y %H:%M", tz = "")
On Mon, Mar 3, 2008 at 11:12 AM, stephen sefick <ssefick at gmail.com> wrote:
> DateTime,Temp,SpCond,DOConc,Depth,pH,ORP,Turbidity+,Chlorophyll,Battery,Cond,DO%,Salinity,TDS
> 01/13/2006 17:01,10.87,84,9.36,0.664,7.3,132,28.8,3.1,11.5,0.062,84.6,0.04,0.055
> 01/13/2006 17:16,10.9,84,9.36,0.66,7.31,133,28.7,2.9,11.5,0.062,84.7,0.04,0.055
> 01/13/2006 17:31,10.92,84,9.36,0.655,7.3,132,28.4,2.6,11.4,0.062,84.8,0.04,0.055
> 01/13/2006 17:46,10.96,85,9.37,0.651,7.3,133,27.5,2.3,11.4,0.062,84.9,0.04,0.055
> 01/13/2006 18:01,10.96,85,9.35,0.644,7.33,132,28.2,2.9,11.4,0.062,84.7,0.04,0.055
> 01/13/2006 18:16,10.98,85,9.35,0.638,7.33,132,27.9,2.5,11.4,0.062,84.8,0.04,0.055
> 01/13/2006 18:31,11.01,85,9.35,0.635,7.31,131,26.4,3,11.5,0.062,84.8,0.04,0.055
> 01/13/2006 18:46,11.04,85,9.33,0.635,7.31,133,26.1,2.9,11.5,0.062,84.7,0.04,0.055
> 01/13/2006 19:01,11.04,85,9.33,0.632,7.33,133,26.3,2.9,11.4,0.062,84.7,0.04,0.055
> 01/13/2006 19:16,11.06,85,9.32,0.639,7.34,134,26,2.6,11.5,0.062,84.6,0.04,0.055
> 01/13/2006 19:31,11.11,85,9.37,0.656,7.35,136,25.6,2.1,11.4,0.062,85.2,0.04,0.055
> 01/13/2006 19:46,11.11,84,9.36,0.65,7.33,137,26.6,2.4,11.4,0.062,85.2,0.04,0.055
> 01/13/2006 20:01,11.18,84,9.41,0.649,7.35,138,27,2.8,11.4,0.062,85.7,0.04,0.055
> 01/13/2006 20:16,11.13,84,9.35,0.651,7.34,140,27.1,2.7,11.4,0.062,85,0.04,0.055
> 01/13/2006 20:31,11.12,84,9.33,0.656,7.32,142,26.7,2.9,11.4,0.062,84.8,0.04,0.055
> 01/13/2006 20:46,11.07,84,9.33,0.667,7.32,143,26.4,3.1,11.4,0.062,84.8,0.04,0.055
> 01/13/2006 21:01,11.05,84,9.35,0.67,7.31,145,26.6,2.7,11.4,0.062,84.9,0.04,0.055
> 01/13/2006 21:16,11.02,84,9.34,0.67,7.32,145,25.9,2.6,11.4,0.062,84.8,0.04,0.055
> 01/13/2006 21:31,11.01,84,9.35,0.673,7.32,146,26.3,3.1,11.4,0.062,84.8,0.04,0.055
> 01/13/2006 21:46,10.99,84,9.35,0.676,7.32,146,26.4,3.3,11.4,0.062,84.8,0.04,0.055
> 01/13/2006 22:01,10.96,84,9.34,0.673,7.32,146,26,2.6,11.5,0.062,84.7,0.04,0.055
> 01/13/2006 22:16,10.94,84,9.32,0.668,7.32,147,26.5,2.9,11.5,0.061,84.4,0.04,0.055
> 01/13/2006 22:31,10.93,84,9.3,0.666,7.31,148,25.8,2.4,11.4,0.062,84.3,0.04,0.055
> 01/13/2006 22:46,10.92,84,9.3,0.666,7.32,148,26,2.2,11.4,0.061,84.2,0.04,0.055
> 01/13/2006 23:01,10.91,84,9.27,0.666,7.32,148,26.5,2.9,11.5,0.062,83.9,0.04,0.055
> 01/13/2006 23:16,10.92,84,9.34,0.666,7.31,149,26.9,2.5,11.4,0.062,84.6,0.04,0.055
> 01/13/2006 23:31,10.93,84,9.31,0.667,7.33,148,25.8,2.8,11.4,0.062,84.3,0.04,0.055
> 01/13/2006 23:46,10.92,84,9.34,0.666,7.31,150,26.1,3.1,11.4,0.062,84.6,0.04,0.055
> 01/14/2006 00:01,10.95,84,9.28,0.667,7.3,144,26.4,2.5,11.5,0.062,84.1,0.04,0.055
> 01/14/2006 00:16,10.95,84,9.29,0.667,7.31,142,25.6,2.3,11.5,0.062,84.2,0.04,0.055
> 01/14/2006 00:31,10.94,84,9.29,0.669,7.3,142,25.6,2.4,11.4,0.062,84.2,0.04,0.055
> 01/14/2006 00:46,10.94,85,9.29,0.671,7.31,139,25.3,3.1,11.4,0.062,84.1,0.04,0.055
> 01/14/2006 01:01,10.95,85,9.26,0.672,7.3,141,25.3,2.6,11.3,0.062,83.9,0.04,0.055
> 01/14/2006 01:16,10.95,85,9.28,0.673,7.31,141,26,3.1,11.4,0.062,84,0.04,0.055
> 01/14/2006 01:31,10.96,85,9.27,0.673,7.34,139,25.4,2.1,11.4,0.062,84,0.04,0.055
> 01/14/2006 01:46,10.95,85,9.28,0.673,7.32,138,24.8,2.5,11.4,0.062,84.1,0.04,0.055
> 01/14/2006 02:01,10.93,85,9.27,0.673,7.31,137,25.1,2.2,11.4,0.062,84,0.04,0.055
> 01/14/2006 02:16,10.93,85,9.28,0.673,7.31,137,24.6,2,11.4,0.062,84.1,0.04,0.055
> 01/14/2006 02:31,10.94,85,9.29,0.671,7.35,133,24.8,2.8,11.4,0.062,84.2,0.04,0.055
> 01/14/2006 02:46,10.91,85,9.27,0.671,7.35,130,24.7,2.1,11.4,0.062,83.8,0.04,0.055
> 01/14/2006 03:01,10.92,85,9.28,0.67,7.32,131,24.5,2.6,11.4,0.062,84,0.04,0.055
> 01/14/2006 03:16,10.91,85,9.26,0.669,7.32,129,24.8,2.6,11.3,0.062,83.8,0.04,0.055
> 01/14/2006 03:31,10.9,85,9.28,0.671,7.32,129,24.5,2.5,11.4,0.062,84,0.04,0.055
> 01/14/2006 03:46,10.86,85,9.28,0.669,7.33,127,23.8,2.5,11.4,0.062,83.9,0.04,0.055
> 01/14/2006 04:01,10.87,85,9.27,0.669,7.33,128,24.3,2.7,11.4,0.062,83.9,0.04,0.055
> 01/14/2006 04:16,10.87,85,9.26,0.668,7.36,125,23.6,2.9,11.3,0.062,83.7,0.04,0.055
> 01/14/2006 04:31,10.8,85,9.28,0.667,7.34,123,23.6,2,11.3,0.062,83.8,0.04,0.056
> 01/14/2006 04:46,10.82,85,9.28,0.666,7.34,125,23.8,2.5,11.4,0.062,83.8,0.04,0.056
> 01/14/2006 05:01,10.82,85,9.26,0.665,7.33,124,24.3,2.3,11.3,0.062,83.7,0.04,0.056
> 01/14/2006 05:16,10.8,86,9.27,0.665,7.33,122,23.8,2.4,11.4,0.062,83.7,0.04,0.056
> 01/14/2006 05:31,10.78,86,9.28,0.667,7.35,122,24.1,3.1,11.3,0.062,83.7,0.04,0.056
> 01/14/2006 05:46,10.76,86,9.27,0.668,7.35,123,23.8,2.8,11.3,0.062,83.6,0.04,0.056
> 01/14/2006 06:01,10.77,86,9.26,0.668,7.34,116,24.2,2.6,11.3,0.062,83.5,0.04,0.056
> x<-read.zoo("SC2.csv", sep="," , format="%m%m/%d%d/%y%y%y%y %h%h:%m%m")
>
> #Error in read.zoo("SC2.csv", sep = ",", format =
> "%m%m/%d%d/%y%y%y%y %h%h:%m%m") :
> index contains NAs Error message
>
> what am I doing wrong SC2.csv is a comma seperated file. The above
> data is right out out of the csv file, which is more like 30,000 rows.
> There are some NA for all of the variables except DateTime for a week
> here and a week there, but I can not find any missing dates.
> thanks
>
> stephen
>
> --
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods. We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
> -K. Mullis
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list