Skip to content

types of vectors / lists

4 messages · Thomas Friedrichsmeier, Erik Iverson, Charilaos Skiadas +1 more

#
Hello,

I am an advanced user of R. Recently I found out that apparently I do 
not fully understand vectors and lists fully
Take this code snippet:


T = c("02.03.2008 12:23", "03.03.2008 05:54")
Times = strptime(T, "%d.%m.%Y %H:%M")
Times                 # OK
class(Times)          # OK
is.list(Times)        # sort of understand and not understand that
length(Times)         # 9 ??? why is it length(Times[1]) ??

Times[1] # OK
Times[[1]] # Wrong


so Times is a vector-style thing of a non-atomic type.
What puzzles me is first that is.list returns true, and more importantly 
that the length-query returns the length of the first object of that 
apparent list and not how many elements are contained (i.e., I would 
have expected 2 as result - and I do not know what syntax to use in 
order to get the 2).

Moreover, if the whole thing is part of a data.frame:

DFTimes = as.data.frame(Times)
dim(DFTimes)
length(DFTimes$Times)  # OK, 2

then everything is as expected.

Could anyone please clearify why is.list returns true and why length in 
the first example returns 9 ? Is it that c() makes a list if the objects 
to be concatenated are not representable directly by atomic types ?

thanks,
Thomas
#
Hello -
a9804814 at unet.univie.ac.at wrote:
?POSIXlt says

Class '"POSIXlt"' is a named list of
      vectors representing

      'sec' 0-61: seconds

      'min' 0-59: minutes

      'hour' 0-23: hours

      'mday' 1-31: day of the month

      'mon' 0-11: months after the first of the year.

      'year' Years since 1900.

      'wday' 0-6 day of the week, starting on Sunday.

      'yday' 0-365: day of the year.

      'isdst' Daylight savings time flag.  Positive if in force, zero if
           not, negative if unknown.
Because Times is a list with 9 elements, try typing names(Times) to see 
what those elements are called.  That will help you understand.
Whether or not this is 'wrong' has been I believe debated before on the 
list.  See this message and the rest of the thread associated with it.

http://tolstoy.newcastle.edu.au/R/help/06/07/31508.html

- Erik Iverson
#
On Mar 5, 2008, at 2:56 PM, a9804814 at unet.univie.ac.at wrote:

            
Far from OK, which is where your misunderstanding starts. Times is a  
POSIXlt object, and behaves as such, not as a vector and not as a  
list. Rather, a bit of both.
The output you see is the result of print(Times), which calls  
print.POSIXlt(Times), which is:

function (x, ...)
{
     print(format(x, usetz = TRUE), ...)
     invisible(x)
}
<environment: namespace:base>


This in turn calls format.POSIXlt(Times), which is pretty complicated  
and I'll omit here, but point is what you see when you type Times is  
not the object TImes itself, but only what the writer of  
print.POSIXlt wants you to see. Similar to the output of lm, which is  
much less than what lm really contains.

Have a look at ?POSIXlt, which says:

Class "POSIXlt" is a named list of vectors representing .... 9  
vectors following.

Try:

names(Times)
Times$sec
Times$min
....

Now perhaps this helps understand the answers?
Not OK at all. This calls [.POSIXlt, look at:

`[.POSIXlt`

In particular, try Times[2], Times[3] etc

Times[[1]] is the same as Times$sec
Times[[2]] is the same as Times$min
and so on.
Haris Skiadas
Department of Mathematics and Computer Science
Hanover College
#
Read R News 4/1 article on dates.
On Wed, Mar 5, 2008 at 2:56 PM, <a9804814 at unet.univie.ac.at> wrote: