[R] subsetting and Dates

Denis Chabot chabot.denis at gmail.com
Thu May 23 23:35:49 CEST 2013


Hi,

I am trying to understand why creating Date variables does not work if I subset to avoid NAs. 

I had problems creating these Date variables in my code and I thought that the presence of NAs was the cause. So I used a condition to avoid NAs.

It turns out that NAs are not a problem and I do not need to subset, but I'd like to understand why subsetting causes the problem.
The strange numbers I start with are what I get when I read an Excel sheet with the function read.xls() from package gdata.  

dat1 = c(41327, 41334, 41341, 41348, 41355, 41362, 41369, 41376, 41383, 41390, 41397)
dat2 = dat1
dat2[c(5,9)]=NA
Data = data.frame(dat1,dat2)

keep1 = !is.na(Data$dat1)
keep2 = !is.na(Data$dat2)


Data$Dat1a = as.Date(Data[,"dat1"], origin="1899-12-30") 
Data$Dat1b[keep1] = as.Date(Data[keep1,"dat1"], origin="1899-12-30") 
Data$Dat2a = as.Date(Data[,"dat2"], origin="1899-12-30") 
Data$Dat2b[keep2] = as.Date(Data[keep2,"dat2"], origin="1899-12-30") 

Data
    dat1  dat2      Dat1a Dat1b      Dat2a Dat2b
1  41327 41327 2013-02-22 15758 2013-02-22 15758
2  41334 41334 2013-03-01 15765 2013-03-01 15765
3  41341 41341 2013-03-08 15772 2013-03-08 15772
4  41348 41348 2013-03-15 15779 2013-03-15 15779
5  41355    NA 2013-03-22 15786       <NA>    NA
6  41362 41362 2013-03-29 15793 2013-03-29 15793
7  41369 41369 2013-04-05 15800 2013-04-05 15800
8  41376 41376 2013-04-12 15807 2013-04-12 15807
9  41383    NA 2013-04-19 15814       <NA>    NA
10 41390 41390 2013-04-26 15821 2013-04-26 15821
11 41397 41397 2013-05-03 15828 2013-05-03 15828

So variables Dat1b and Dat2b are not converted to Date class.


sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/fr_CA.UTF-8/C/fr_CA.UTF-8/fr_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gdata_2.12.0

loaded via a namespace (and not attached):
[1] gtools_2.7.0

Thanks in advance,

Denis


More information about the R-help mailing list