Dear all, I am trying to identify the days absent in a vector so as to be able to display missing data in the graphics in .It should be done automatically since the graphics will be generated for different periods of the year. I have been trying to do a function below but it did not work. I really appreciate any help ordados <- function(datas,var) { ind <- !datas %in% var # find the missing date in var miss <- datas[ind] mat <- matrix(NA,length(miss),length(miss)) #insert NA in the missing date mat[,1]<-miss jundado <- rbind (mat,var) jundado <- jundado[order(jundado[,1]),] # sort by dates } For instance: If I have the vector below: 2009-12-01 26.8 2009-12-03 27.9 2009-12-04 25.6 2009-12-05 20.8 2009-12-08 20.8 I will need a result like this one 2009-12-01 26.8 2009-12-02 NA 2009-12-03 27.9 2009-12-04 25.6 2009-12-05 20.8 2009-12-06 NA 2009-12-07 NA 2009-12-08 20.8 Bye and Best Regards Nilza Barros [[alternative HTML version deleted]]
Hi: # Create a sequence of consecutive dates and assign to a data frame dates <- data.frame(day = seq(as.Date('2009-12-01'), by = 'days', length 10)) # Let's call your data frame (not vector) 'df'; to make sure that things are # compatible, names(df) <- c('day', 'y') df$day <- as.Date(df$day) # Then,> merge(dates, df, all.x =TRUE)day y 1 2009-12-01 26.8 2 2009-12-02 NA 3 2009-12-03 27.9 4 2009-12-04 25.6 5 2009-12-05 20.8 6 2009-12-06 NA 7 2009-12-07 NA 8 2009-12-08 20.8 9 2009-12-09 NA 10 2009-12-10 NA HTH, Dennis On Tue, Feb 9, 2010 at 4:30 PM, Nilza BARROS <nilzabarros@gmail.com> wrote:> Dear all, > > I am trying to identify the days absent in a vector so as to be able to > display missing data in the graphics in .It should be done automatically > since the graphics will be generated for different periods of the year. > I have been trying to do a function below but it did not work. I really > appreciate any help > > ordados <- function(datas,var) > { > ind <- !datas %in% var # find the missing date in var > miss <- datas[ind] > mat <- matrix(NA,length(miss),length(miss)) #insert NA in the missing date > mat[,1]<-miss > jundado <- rbind (mat,var) > jundado <- jundado[order(jundado[,1]),] # sort by dates > } > For instance: > If I have the vector below: > 2009-12-01 26.8 > 2009-12-03 27.9 > 2009-12-04 25.6 > 2009-12-05 20.8 > 2009-12-08 20.8 > I will need a result like this one > > 2009-12-01 26.8 > 2009-12-02 NA > 2009-12-03 27.9 > 2009-12-04 25.6 > 2009-12-05 20.8 > 2009-12-06 NA > 2009-12-07 NA > 2009-12-08 20.8 > > Bye and Best Regards > Nilza Barros > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Tena koe Nilza I think merge() will do what you want. For example:> completeDatesV1 1 01-12-09 2 02-12-09 3 03-12-09 4 04-12-09 5 05-12-09 6 06-12-09 7 07-12-09 8 08-12-09> testDataV1 V2 1 01-12-09 26.8 2 03-12-09 27.9 3 04-12-09 25.6 4 05-12-09 20.8 5 08-12-09 20.8> merge(completeDates, testData, by=1, all=T)V1 V2 1 01-12-09 26.8 2 02-12-09 NA 3 03-12-09 27.9 4 04-12-09 25.6 5 05-12-09 20.8 6 06-12-09 NA 7 07-12-09 NA 8 08-12-09 20.8 HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Nilza BARROS > Sent: Wednesday, 10 February 2010 1:30 p.m. > To: r-help at r-project.org > Subject: [R] manipulate missing dates in a graphics a vector > > Dear all, > > I am trying to identify the days absent in a vector so as to > be able to display missing data in the graphics in .It > should be done automatically since the graphics will be > generated for different periods of the year. > I have been trying to do a function below but it did not > work. I really appreciate any help > > ordados <- function(datas,var) > { > ind <- !datas %in% var # find the missing date in var miss <- > datas[ind] mat <- matrix(NA,length(miss),length(miss)) > #insert NA in the missing date mat[,1]<-miss jundado <- > rbind (mat,var) jundado <- jundado[order(jundado[,1]),] # > sort by dates } For instance: > If I have the vector below: > 2009-12-01 26.8 > 2009-12-03 27.9 > 2009-12-04 25.6 > 2009-12-05 20.8 > 2009-12-08 20.8 > I will need a result like this one > > 2009-12-01 26.8 > 2009-12-02 NA > 2009-12-03 27.9 > 2009-12-04 25.6 > 2009-12-05 20.8 > 2009-12-06 NA > 2009-12-07 NA > 2009-12-08 20.8 > > Bye and Best Regards > Nilza Barros > > [[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. >