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向量做一些操作,但是当我将它传递给一个函数时,这个向量会变成一个数字向量,而不是保持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? I know that POSIXct
is numeric
under the hood, but I want to retain the original class for readability.
为什么会发生?如何保留原向量的POSIXct类?我知道POSIXct在外壳下是数字的,但是我想保留原来的类以使其具有可读性。
1 个解决方案
#1
2
We can use lapply
instead of sapply
as sapply
by default has the option 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
.
我们可以使用lapply代替sapply,因为默认情况下,sapply的选项是simplify = TRUE。因此,如果列表元素的长度相同,它将根据列表元素的长度将其简化为向量或矩阵,POSIXct将存储为数字。
lst <- lapply(dates, function(x) x)
If we need to use sapply
, then an option would simplify = FALSE
如果我们需要使用sapply,那么一个选项将简单化= 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") )
#1
2
We can use lapply
instead of sapply
as sapply
by default has the option 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
.
我们可以使用lapply代替sapply,因为默认情况下,sapply的选项是simplify = TRUE。因此,如果列表元素的长度相同,它将根据列表元素的长度将其简化为向量或矩阵,POSIXct将存储为数字。
lst <- lapply(dates, function(x) x)
If we need to use sapply
, then an option would simplify = FALSE
如果我们需要使用sapply,那么一个选项将简单化= 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") )