R: Why is the Date class lost after a subset

Here is a simple example. I have 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 am adding a column consisting of the same records:

 for(i in 1:3){ Data[i, "date2"] <- Data[i, "date"] } 

The result is as follows:

  date date2 1 1970-01-01 0 2 1970-01-02 1 3 1970-01-03 2 

For unknown reasons, the date2 column class is numeric instead of the date, which was the date class. Curiously, if you explicitly specify R to use the date format:

 for(i in 1:3){ Data[i, "date3"] <- as.Date(Data[i, "date"]) } 

it does not matter.

  date date2 date3 1 1970-01-01 0 0 2 1970-01-02 1 1 3 1970-01-03 2 2 

The problem is to use a subset of [], in more interesting examples where you have two date columns and you want to create a third one that selects a date from one of the other two columns, depending on some factor, the same thing happens.

Of course, we can fix everything retrospectively 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 remain dates when moving to another column?

+8
r
source share
1 answer

This is not a final decision, but I think it can help to understand.

Here is your details:

 Data <- data.frame(date = as.Date(c('2000/01/01', '2012/01/02', '2013/01/03'))) 

Take these 2 vectors, one of which is specified by default as a number, and the second as Date.

 vv <- vector("numeric",3) vv.Date <- vector("numeric",3) class(vv.Date) <- 'Date' vv [1] 0 0 0 > vv.Date [1] "1970-01-01" "1970-01-01" "1970-01-01" ## type dates is initialized by the origin 01-01-1970 

Now, if I try to assign the first element of each vector, as you do in the first step of the loop:

 vv[1] <- Data$date[1] vv.Date[1] <- Data$date[1] vv [1] 10957 0 0 > vv.Date [1] "2000-01-01" "1970-01-01" "1970-01-01" 

As you can see, a typed vector is well created. What happens when you assign a vector to a scalar value, R tries to internally convert it to a vector type . To return to your example when you do this:

You create a numeric vector (vv) and you are trying to assign dates to it:

 for(i in 1:3){ Data[i, "date3"] <- as.Date(Data[i, "date"]) } 

If you type date 3, for example:

 Data$date3 <- vv.Date 

then you will try again

 for(i in 1:3){ Data[i, "date3"] <- as.Date(Data[i, "date"]) } 

You will get a good result:

  date date3 1 2000-01-01 2000-01-01 2 2012-01-02 2012-01-02 3 2013-01-03 2013-01-03 
+6
source share

All Articles