[R] types of vectors / lists

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 5 22:00:00 CET 2008


Read R News 4/1 article on dates.

On Wed, Mar 5, 2008 at 2:56 PM,  <a9804814 at unet.univie.ac.at> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list