[R] format for as.Date and inserting missing rows in a data frame

Stefano Sofia stefano.sofia at regione.marche.it
Mon Mar 10 12:59:13 CET 2014


Thank you to Arun and Petr for the give hints, veru useful.
In order to get the date output in the required format, these are the commands that works:

dat1$Date <- as.Date(paste(dat1$Y_init, dat1$M_init, dat1$D_init, sep=" "), format="%Y %m %d")

dat1$Date1 <- strptime(as.character(dat1$Date), "%Y-%m-%d")
dat1$Date1txt <- format(dat1$Date1, "%Y%m%d")

Stefano



________________________________________
Da: arun [smartpink111 at yahoo.com]
Inviato: martedì 4 marzo 2014 17.06
A: r-help at r-project.org
Cc: Stefano Sofia
Oggetto: Re: [R] format for as.Date and inserting missing rows in a data frame

Hi,
May be this helps:
dat <- read.table(text="Raingouge_number Station_number Year Month Day Rainfall
2004 2230 1951 1 1 2.60
2004 2230 1951 1 2 0.40
2004 2230 1951 1 3 0.00
2004 2230 1951 1 4 0.00
2004 2230 1951 1 5 0.20
2004 2230 1951 1 6 0.00
2004 2230 1951 1 7 0.00
2004 2230 1951 1 9 0.00
2004 2230 1951 1 10 0.00
2004 2230 1951 1 11 0.20",sep="",header=TRUE)
dat <-  within(dat,Date <- as.Date(paste(Year,Month,Day),format="%Y %m %d"))
dat2 <- data.frame(Date=seq(dat$Date[1],dat$Date[length(dat$Date)],by="day"))
 res <- merge(dat,dat2,all=TRUE)
res$Rainfall[is.na(res$Rainfall)] <- -999
res
A.K.




On Tuesday, March 4, 2014 5:58 AM, Stefano Sofia <stefano.sofia at regione.marche.it> wrote:
Dear R users,
I have a very long data frame (50 years, more than 1.5 million rows) of daily rainfall data from about 80 raingouges.
The data frame that I have been given looks like

Raingouge_number Station_number Year Month Day Rainfall
2004 2230 1951 1 1 2.60
2004 2230 1951 1 2 0.40
2004 2230 1951 1 3 0.00
2004 2230 1951 1 4 0.00
2004 2230 1951 1 5 0.20
2004 2230 1951 1 6 0.00
2004 2230 1951 1 7 0.00
2004 2230 1951 1 8 0.00
2004 2230 1951 1 9 0.00
2004 2230 1951 1 10 0.00
...

There could be some missing days. I have two questions.
1st question:
In order to handle eventual missing days I think that I have to transform three separate numbers (Year, Month and Day) to Date.
Is there a format in as.Date suitable for this transformation or before all I have to set all the months and days to two digits, remove spaces and then apply
as.Date with format "%Y%m%d"?

2nd question
In case of missing day, the corresponding row will be missing and then I have to insert this new row and put -999.9 as Rainfall. Is there an easy way to do that?


Thank you for your help
Stefano

________________________________

AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere informazioni confidenziali, pertanto è destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si è il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed urgenza, la risposta al presente messaggio di posta elettronica può essere visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.


More information about the R-help mailing list