[R] [[]] confusion

Duncan Murdoch murdoch.duncan at gmail.com
Tue Feb 15 19:23:34 CET 2011


On 15/02/2011 11:40 AM, Sam Steingold wrote:
> what does the output for [[]] mean here:
> >  all$X.Time[5]
> [1] "2011-02-15 09:32:26.37222"
> >  all$X.Time[[5]]
>   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

You didn't tell us what X.Time was, but it looks like a POSIXlt object, 
which is stored internally as a list of 9 vectors.  You're seeing the 
5th of those, which should be the month number (Jan=0, Feb=1, ...).
> >  all$X.Time[1]
> [1] "2011-02-15 09:31:29.18761"
> >  all$X.Time[[1]]
>   [1] 29.18761 34.30949 36.38144 12.28500 26.37222 47.00837 40.20271 32.83765
>   [9] 54.56998 28.56961 55.96641 28.91920 32.29962 10.94081 34.31731  3.21796
> [17] 58.12660 19.69056 55.99009 45.76731 46.52072

The first vector is the second.
> also,
> >  all$X.Time[0]
> character(0)
>
> why no error? I thought that vectors were 1-based.

Yes, but indexing by 0 or negative numbers has special meaning:  see the 
R Language Definition manual.

Duncan Murdoch



More information about the R-help mailing list