Stefano Sofia
2014-Mar-04 10:57 UTC
[R] format for as.Date and inserting missing rows in a data frame
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. [[alternative HTML version deleted]]
arun
2014-Mar-04 16:06 UTC
[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. ??? [[alternative HTML version deleted]] ______________________________________________ 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.
PIKAL Petr
2014-Mar-05 05:36 UTC
[R] format for as.Date and inserting missing rows in a data frame
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Stefano Sofia > Sent: Tuesday, March 04, 2014 11:58 AM > To: r-help at r-project.org > Subject: [R] format for as.Date and inserting missing rows in a data > frame > > 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"?This shall do it if you put dataframe name instead of ... as.Date(paste(...$Year, ...$Month, ...$Day, sep="."), 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?Why? What is wrong with NA? What do you want to do with -999.9? Anyway, you can get sequence of dates ?seq.Date and merge it with your data. ?merge I do not help you to put -999.9 instead of NA as I consider it extremely silly. Regards Petr> > > 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. > > [[alternative HTML version deleted]]________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.