Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is POSIXct converted to numeric when converting list to vector

Tags:

r

posixct

I have a list redDressTweets of statuses. status is a class with a field created. I am trying to form a list of times in which tweets were posted. Here is how I'm trying to do it

times <- unlist(lapply(redDressTweets, function(tweet) {tweet$created }))

The output is a vector of numbers:

[1] 1478044029 1477954062 1477909847 1477887746 1477832560 1477640940 1477640939
[8] 1477628031 1477540826

But the class of redDressTweets[[1]]$created is "POSIXct" "POSIXt".

Why is this happening and how do I stop it from converting POSIXct to numeric?

like image 389
wlad Avatar asked Nov 02 '16 11:11

wlad


People also ask

How to convert a list to vector in R?

unlist() function in R Language is used to convert a list to vector. It simplifies to produce a vector by preserving all components. Syntax: unlist(list) Parameters: list: It is a list or Vector use.name: Boolean value to prserve or not the position names. Example 1: Converting list numeric vector into a single vector

What is the use of posixct?

The as.POSIXct () is one of the Date-Time conversion functions. It belongs to as.POSIX* class provides the functions to manipulate objects of classes “POSIXlt” and “POSIXct” representing calendar dates and times. Here, the output numeric value you get shows the number of seconds since an arbitrary date, usually 1/1/1970.

How to convert date to numeric format in R?

To convert Date to Numeric format in R, use the as.POSIXct () function and then you can coerce it to a numeric value using as.numeric () function. The as.POSIXct () is one of the Date-Time conversion functions.

Is posixct a time only class?

Posixct is not a time only class, POSIXct represents the (signed) number of seconds since the beginning of 1970 (in the UTC time zone) as a numeric vector. It's a datetime class. I tried using this instead, adding this at the end of my code it didn't work. Any idea where i am going wrong please?


1 Answers

You can reproduce more easily like this:

x <- list(as.POSIXct("2016-11-02"), as.POSIXct("2016-11-03"))
unlist(x)
#[1] 1478041200 1478127600

unlist combines the values inside the list. The internal representation of POSIXct variables are numeric values. They only are a POSIXct variable due to attributes, most importantly the class attribute. A list can hold all kinds of objects. The documentation says:

Where possible the list elements are coerced to a common mode during the unlisting, and so the result often ends up as a character vector. Vectors will be coerced to the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression: pairlists are treated as lists.

Note that it says "common mode", not common class. Since a class could be anything and could enforce any kind of underlying structure (e.g., it might not even be possible to combine two objects of the same class in a meaningful way), unlist just strips all attributes (except for a list where all elements are factors). It would be possible to handle POSIXct values like factor values, but that's not the case (and might have performance implications).

You can't avoid this, but fix it easily:

y <- unlist(x)
attributes(y) <- attributes(x[[1]])
y
#[1] "2016-11-02 CET" "2016-11-03 CET"

Obviously this assumes that all list elements have the same timezone attribute.

like image 165
Roland Avatar answered Nov 14 '22 22:11

Roland