Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does is.vector() return TRUE for list?

Tags:

I'm an R beginner. Browsing the R documentation, I stumbled upon this sentence ?is.vector: "If mode = "any", is.vector may return TRUE for the atomic modes, list and expression."

I'm just curious - why? All of the documentation I've read states that lists and vectors are two different data types. Is there some deeper R datatype concept I'm not getting?

like image 430
Ram Ahluwalia Avatar asked May 17 '11 14:05

Ram Ahluwalia


1 Answers

A list is (in most cases) itself a vector. From the help files for ?list: "Most lists in R internally are Generic Vectors, whereas traditional dotted pair lists (as in LISP) are available but rarely seen by users (except as formals of functions)."

This means you can use vector to pre-allocate memory for a list:

x <- vector("list", 3) class(x) [1] "list" 

Now allocate a value to the second element in the list:

x[[2]] <- 1:5  x  [[1]] NULL  [[2]] [1] 1 2 3 4 5  [[3]] NULL 

See ?list and ?vector for more details.

like image 83
Andrie Avatar answered Nov 10 '22 11:11

Andrie