R: Why is class Date lost upon subsetting

R: Why is class Date lost upon subsetting

Here is an easy example. I have a a data frame with three dates in it:
Data < - as.data.frame(as.Date(c('1970/01/01', '1970/01/02', '1970/01/03')))
names(Data) <- "date"

Now I add a column consisting of the same entries:
for(i in 1:3){
  Data[i, "date2"] <- Data[i, "date"]
}

Output looks like this:
        date date2
1 1970-01-01     0
2 1970-01-02     1
3 1970-01-03     2

For unknown reasons the class of column date2 is numeric instead of date which was the class of date. Curiously, if you tell R explicitly to use the Date format:
for(i in 1:3){
  Data[i, "date3"] <- as.Date(Data[i, "date"])
}

it doesn't make any difference. 
        date date2 date3
1 1970-01-01     0     0
2 1970-01-02     1     1
3 1970-01-03     2     2

The problem seems to be in the use of subsetting [], in more interesting examples where you have two columns of dates and want to create a third one that picks a date from one of the two other columns depending on some factor the same happens.
Of course we can fix everything in retrospect by doing something like:
Data$date4 <- as.Date(Data$date2, origin = "1970-01-01")

but I'm still wondering: why? Why is this happening? Why can't my dates just stay dates when being transferred to another column??

Solutions/Answers:

Solution 1:

This is not a final solution, but

Read more