[R] two questions for R beginners

hadley wickham h.wickham at gmail.com
Mon Mar 1 17:33:14 CET 2010

> 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:

> 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


Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University

More information about the R-help mailing list