为什么mapply不返回日期对象?

时间:2022-05-18 08:46:24

I have a function that takes a Date-object and returns one. However, when I applied the function to a data.frame column using the mapply function, I ran into problems: I didn't get Date-objects back, as expected, but numbers. Any idea how I could convert those to Date-objects? Also, I would be interested in what's happening here. Help is really appreciated!

我有一个函数,它接受一个Date对象并返回一个。但是,当我使用mapply函数将函数应用于data.frame列时,我遇到了问题:我没有像预期的那样得到Date-objects,而是数字。知道如何将它们转换为Date对象吗?另外,我会对这里发生的事情感兴趣。非常感谢帮助!

Minimal example:

最小的例子:

#Define simple function that takes a date-object and returns a date-object
add_day <- function(dat) {return(dat + 1)}

#Set up data.frame with two date-object entries in one column
df <- data.frame(Col_A = c(as.Date("01/01/00", "%m/%d/%y"), as.Date("05/02/11", "%m/%d/%y")))
#That is the desired result: give a date-object to the function, get one back
add_day(df[1, "Col_A"]) #Returns [1] "2000-01-02"
add_day(df[2, "Col_A"]) #Returns [1] "2011-05-03"

#Why does it not work here? What do I get back?
mapply(add_day, df[, "Col_A"]) #Returns [1] 10958 15097; Why? What is that?

3 个解决方案

#1


14  

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.

如果你将你的mapply调用包装在as.Date中,你将看到你期望的输出。

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

要了解这里发生了什么,请考虑mapply在引擎盖下使用sapply。例如:

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是为了方便而未列出的结果。如果我们指定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.

因此,当R将列表强制转换为向量时,类信息将被删除,只保留内部存储,即自[具体日期]以来的天数。当然,mapply也有一个SIMPLIFY参数以相同的方式起作用。

#2


12  

Another option is something like sapply.preserving.attributes:

另一个选项是sapply.preserving.attributes:

sapply.preserving.attributes = function(l, ...) {
    r = sapply(l, ...)
    attributes(r) = attributes(l)
    r
}

> sapply.preserving.attributes(dates, add_day)
[1] "2000-01-02" "2011-05-03"

#3


0  

Can use this single line of code after you run mapply

运行mapply后可以使用这一行代码

df$date <- as.Date(as.numeric(df$date))

df $ date < - as.Date(as.numeric(df $ date))

#1


14  

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.

如果你将你的mapply调用包装在as.Date中,你将看到你期望的输出。

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

要了解这里发生了什么,请考虑mapply在引擎盖下使用sapply。例如:

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是为了方便而未列出的结果。如果我们指定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.

因此,当R将列表强制转换为向量时,类信息将被删除,只保留内部存储,即自[具体日期]以来的天数。当然,mapply也有一个SIMPLIFY参数以相同的方式起作用。

#2


12  

Another option is something like sapply.preserving.attributes:

另一个选项是sapply.preserving.attributes:

sapply.preserving.attributes = function(l, ...) {
    r = sapply(l, ...)
    attributes(r) = attributes(l)
    r
}

> sapply.preserving.attributes(dates, add_day)
[1] "2000-01-02" "2011-05-03"

#3


0  

Can use this single line of code after you run mapply

运行mapply后可以使用这一行代码

df$date <- as.Date(as.numeric(df$date))

df $ date < - as.Date(as.numeric(df $ date))