r - Specify custom Date format for colClasses argument in read.table/read.csv -


question:

is there way specify date format when using colclasses argument in read.table/read.csv?

(i realise can convert after importing, many date columns this, easier in import step)


example:

i have .csv date columns in format %d/%m/%y.

dataimport <- read.csv("data.csv", colclasses = c("factor","factor","date")) 

this gets conversion wrong. example, 15/07/2008 becomes 0015-07-20.


reproducible code:

data <-  structure(list(func_loc = structure(c(1l, 2l, 3l, 3l, 3l, 3l,  3l, 4l, 4l, 5l), .label = c("3076wag0003", "3076wag0004", "3076wag0007",  "3076wag0009", "3076wag0010"), class = "factor"), order_type = structure(c(3l,  3l, 1l, 1l, 1l, 1l, 2l, 2l, 3l, 1l), .label = c("pm01", "pm02",  "pm03"), class = "factor"), actual_finish = structure(c(4l, 6l,  1l, 2l, 3l, 7l, 1l, 8l, 1l, 5l), .label = c("", "11/03/2008",  "14/08/2008", "15/07/2008", "17/03/2008", "19/01/2009", "22/09/2008",  "6/09/2007"), class = "factor")), .names = c("func_loc", "order_type",  "actual_finish"), row.names = c(na, 10l), class = "data.frame")   write.csv(data,"data.csv", row.names = f)                                                          dataimport <- read.csv("data.csv") str(dataimport) dataimport  dataimport <- read.csv("data.csv", colclasses = c("factor","factor","date")) str(dataimport) dataimport 

and here's output looks like:

code output

you can write own function accepts string , converts date using format want, use setas set as method. can use function part of colclasses.

try:

setas("character","mydate", function(from) as.date(from, format="%d/%m/%y") )  tmp <- c("1, 15/08/2008", "2, 23/05/2010") con <- textconnection(tmp)  tmp2 <- read.csv(con, colclasses=c('numeric','mydate'), header=false) str(tmp2) 

then modify if needed work data.

edit ---

you might want run setclass('mydate') first avoid warning (you can ignore warning, can annoying if lot , simple call gets rid of it).


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -