Why Does Mapply Not Return Date-Objects

Why does mapply not return date-objects?

Your function is returning 'dates', just not in the format you are used to. Dates are stored internally as days since [some fixed date]. (I can't remember off the top of my head which one, and varies slightly by specific format.)

If you wrap your mapply call in as.Date you'll see the output you expect.

To see what's going on here, consider that mapply is using sapply under the hood. So for example:

sapply(df[,1],add_day)
[1] 10958 15097

But remember that sapply by default is unlisting results for convenience. If we specify simplify = FALSE:

sapply(df[,1],add_day,simplify = FALSE)
[[1]]
[1] "2000-01-02"

[[2]]
[1] "2011-05-03"

So when R coerced the list to a vector, the class information is dropped and only the internal storage is retained, namely the number of days since [whatever that specific date is]. And of course, mapply also has a SIMPLIFY argument that acts in the same way.

Returning a Date from a Function Using sapply()

This looks way too complicated. If I understand correctly, you can simply do this:

x <- c("1072015", "11072015")
as.Date(formatC(as.integer(x), width = 8, flag = 0), format = "%d%m%Y")
#[1] "2015-07-01" "2015-07-11"

How to return a data frame when using lapply with date and time object

You can use replace.

ind <- which(ID %in% c(101, 102, 103))
df$timestamp <- with(df,
replace(timestamp
, ind
, timestamp[ind] + 60))

In tidyverse, you can use case_when.

library(tidyverse)
df <- df %>%
mutate(timestamp = case_when(ID %in% c(101, 102, 103) ~ timestamp + 60,
T ~ timestamp))

Why does Date.parse not return a Date object?

To answer the question in the title: Because they decided so when creating the JavaScript language. Probably because Java's java.util.Date parse function was doing the same thing, and they wanted to mimic its behavior to make the language feel more familiar.

To answer the question in the text... Use this construct to get two date objects:

var today2 = new Date(Date.parse("2008-10-28"));

EDIT: A simple

var today2 = new Date("2008-10-28");

also works.


Note: Old Internet Explorer versions (anything before 9) does not understand dashes in the date string. It works with slashes, though:

var today2 = new Date("2008/10/28");

Slashes seem to be universally understood by browsers both old and new.

R: Why is class Date lost upon subsetting

This is not a final solution, but I think that can help to understand.

Here your data :

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

Take this 2 vectors , one typed by default as numeric 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 your 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 see the typed vector is well created. What happen, when you assign a vector by a scalar value , R try internally to convert it to the type of the vector. To return to your example, When you do this :

You a creating a numeric vector (vv), and you try to assign dates to it:

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

If you type your date3 , for example:

Data$date3 <- vv.Date

then you 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


Related Topics



Leave a reply



Submit