[R] two questions for R beginners

Duncan Murdoch murdoch at stats.uwo.ca
Mon Mar 1 19:41:55 CET 2010


On 01/03/2010 11:33 AM, hadley wickham wrote:
> > Suppose X is a dataframe or a matrix.  What would you expect to get from
> > X[1]?  What about as.vector(X), or as.numeric(X)?
> >
> > The point is that a dataframe is a list, and a matrix isn't.  If users don't
> > understand that, then they'll be confused somewhere.  Making matrices more
> > list-like in one respect will just move the confusion elsewhere.  The
> > solution is to understand the difference.
>
> What I find more confusing is the behaviour of $ with vectors.  In my
> mind x$a is a shortcut for writing x[["a"]], but:
>   

And I still remember being surprised by the x[["a"]] behaviour!

Duncan Murdoch
> > x <- list(a = 1)
> > x$a
> [1] 1
> > x <- c(a = 1)
> > x$a
> Error in x$a : $ operator is invalid for atomic vectors
> > x[["a"]]
> [1] 1
>
> Hadley
>
>



More information about the R-help mailing list