简体   繁体   English

将POSIXct对象传递给函数会返回数字向量

[英]Passing POSIXct object to function returns numeric vector

I'm trying to do some manipulation on the POSIXct vector, but when I pass it to a function, the vector changes into a numeric vector, instead of retaining POSIXct class, even when the function itself only returns the object: 我正在尝试对POSIXct向量进行一些操作,但是当我将它传递给函数时,向量会变为numeric向量,而不是保留POSIXct类,即使函数本身只返回对象:

# Sample dates from vector and it's class.
> dates <- as.POSIXct(c("2012-02-01 12:32:00", "2012-10-24 17:25:56", "2008-09-26 17:13:31", "2011-08-23 11:11:17,", "2015-09-19 22:28:33"), tz = "America/Los_Angeles")
> dates
[1] "2012-02-01 12:32:00 PST" "2012-10-24 17:25:56 PDT" "2008-09-26 17:13:31 PDT" "2011-08-23 11:11:17 PDT" "2015-09-19 22:28:33 PDT"
> class(dates)
[1] "POSIXct" "POSIXt" 
# Simple subset is retaining original class.
> qq <- dates[1:5]
> qq
[1] "2012-02-01 12:32:00 PST" "2012-10-24 17:25:56 PDT" "2008-09-26 17:13:31 PDT" "2011-08-23 11:11:17 PDT" "2015-09-19 22:28:33 PDT"
> class(qq)
[1] "POSIXct" "POSIXt" 
# sapply on the same subset using simple "return" function changes class to "numeric" - why? How to retain "POSIXct"?
> qq2 <- sapply(dates[1:5], function(x) x)
> qq2
[1] 1328128320 1351124756 1222474411 1314123077 1442726913
> class(qq2)
[1] "numeric"

Why it happens? 为什么会这样? How can I retain the POSIXct class of the original vector? 如何保留原始向量的POSIXct类? I know that POSIXct is numeric under the hood, but I want to retain the original class for readability. 我知道POSIXct POSIXct numeric ,但我想保留原始类以提高可读性。

We can use lapply instead of sapply as sapply by default has the option simplify = TRUE . 我们可以用lapply代替sapply作为sapply默认情况下有以下选项simplify = TRUE So, if the list elements are of the same length, it will simplify it to vector or matrix depending on the length of the list elements and POSIXct is stored as numeric . 因此,如果list的元素具有相同的长度,它将它简化到vectormatrix取决于长度list元素和POSIXct被存储为numeric

lst <- lapply(dates, function(x) x)

If we need to use sapply , then an option would simplify = FALSE 如果我们需要使用sapply ,那么选项将simplify = FALSE

lst <- sapply(dates, function(x) x, simplify=FALSE)

After applying the function, if we need as a vector output, 应用函数后,如果我们需要作为矢量输出,

do.call("c", lst)

Regarding the change of timezone, it is documented in the ?DateTimeClasses 关于时区的变化,它记录在?DateTimeClasses

Using c on "POSIXlt" objects converts them to the current time zone, and on "POSIXct" objects drops any "tzone" attributes (even if they are all marked with the same time zone). 在“POSIXlt”对象上使用c将它们转换为当前时区,在“POSIXct”对象上删除任何“tzone”属性(即使它们都标有相同的时区)。

So, the possible option would be (as mentioned in the comments by @kmo) 所以,可能的选择是(正如@kmo的评论中所提到的)

.POSIXct(lst, tz = "America/Los_Angeles")
#[1] "2012-02-01 12:32:00 PST" "2012-10-24 17:25:56 PDT" "2008-09-26 17:13:31 PDT" "2011-08-23 11:11:17 PDT" "2015-09-19 22:28:33 PDT"

Or as @thelatemail mentioned in the comments 或者@thelatemail在评论中提到

.POSIXct(sapply(dates,I), attr(dates,"tzone") )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM