Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between vector and list data types in R?

Tags:

list

r

vector

People also ask

What is vector data type in R?

Advertisements. Vectors are the most basic R data objects and there are six types of atomic vectors. They are logical, integer, double, complex, character and raw.

Can a vector have different data types in R?

R has a wide variety of data types including scalars, vectors (numerical, character, logical), matrices, data frames, and lists.

Which is better list or vector?

In general, use vector when you don't care what type of sequential container that you're using, but if you're doing many insertions or erasures to and from anywhere in the container other than the end, you're going to want to use list. Or if you need random access, then you're going to want vector, not list.

What is the difference between vector and linked list?

A linked list has a more complex data structure than a vector; each of its elements consists of the data itself and then one or more pointers. A pointer is a variable that contains a memory address. In the case of a singly linked list, there will be just one pointer referencing the address of the next element.


Technically lists are vectors, although very few would use that term. "list" is one of several modes, with others being "logical", "character", "numeric", "integer". What you are calling vectors are "atomic vectors" in strict R parlance:

 aaa <- vector("list", 3)
 is.list(aaa)   #TRUE
 is.vector(aaa)  #TRUE

Lists are a "recursive" type (of vector) whereas atomic vectors are not:

is.recursive(aaa)  # TRUE
is.atomic(aaa)  # FALSE

You process data objects with different functions depending on whether they are recursive, atomic or have dimensional attributes (matrices and arrays). However, I'm not sure that a discussion of the "advantages and disadvantages" of different data structures is a sufficiently focused question for SO. To add to what Tommy said, besides lists being capable of holding an arbitrary number of other vectors there is the availability of dataframes which are a particular type of list that has a dimensional attribute which defines its structure. Unlike matrices and arrays which are really folded atomic objects, dataframes can hold varying types including factor types.

There's also the caveat that the is.vector function will return FALSE when there are attributes other than names. See: what is vector?


Lists are "recursive". This means that they can contain values of different types, even other lists:

x <- list(values=sin(1:3), ids=letters[1:3], sub=list(foo=42,bar=13))
x # print the list
x$values   # Get one element
x[["ids"]] # Another way to get an element
x$sub$foo  # Get sub elements
x[[c(3,2)]]  # Another way (gets 13)
str(x)     # A "summary" of the list's content

Lists are used in R to represent data sets: the data.frame class is essentially a list where each element is a column of a specific type.

Another use is when representing a model: the result from lm returns a list that contains a bunch of useful objects.

d <- data.frame(a=11:13, b=21:23)
is.list(d) # TRUE
str(d)

m <- lm(a ~ b, data=d)
is.list(m) # TRUE
str(m)

Atomic vectors (non-list like, but numeric, logical and character) are useful since all elements are known to have the same type. This makes manipulating them very fast.


As someone who's just gotten into R, but comes from a C/Java/Ruby/PHP/Python background, here's how I think of it.

A list is really an array + a hashmap. It's a PHP associative array.

> foo = list(bar='baz')
> foo[1]
'baz'
> foo$bar
'baz'
> foo[['bar']]
'baz'

A vector is a fixed-type array/list. Think of it like a linked list - because putting dissimilar items into a linked list is an anti-pattern anyways. It's a vector in the same sense that SIMD/MMX/vector units use the word.


This and similar introductory questions are answered in http://www.burns-stat.com/pages/Tutor/hints_R_begin.html

It is meant to be a gentle introduction that gets you up and running with R as quickly as possible. To some extent it succeeds.

--- Edit: --

An attempt to explain further; quoted from the above reference.

Atomic vector

There are three varieties of atomic vector that you are likely to encounter:

  • “numeric”
  • “logical”
  • “character”

The thing to remember about atomic vectors is that all of the elements in them are only of one type.

List

Lists can have different types of items in different components. A component of a list is allowed to be another list , an atomic vector (and other things).

Please also refer to this link.