What are the differences between vector and list d

2019-01-10 03:04发布

问题:

What are the main differences between vector and list data types in R? What are the advantages or disadvantages of using (or not) these two data types?

I would appreciate seeing examples that demonstrate the use cases of the data types.

回答1:

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" in strict R parlance:

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

Lists are a "recursive" type 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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

Vectors (one-dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type.

A list in R is similar to your to-do list at work or school: the different items on that list most likely differ in length, characteristic, type of activity that has to do be done, ...

A list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other in any way.

You could say that a list is some kind super data type: you can store practically any piece of information in it!

To construct a list you use the function list(): my_list <- list(comp1, comp2 ...)

The arguments to the list function are the list components. Remember, these components can be matrices, vectors, other lists, ...

To summarize, the main difference between list and vector is that list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other in any way... while the elements in a vector all have the same data type.



回答6:

list include multiple data types like character, numeric, logical et. but vector only contains similar type of data. for ex:

scores <- c(20,30,40,50)
student <- c("A","B","C","D")
sc_log <- c(TRUE,FALSE,FALSE,TRUE)

for list:

mylist <- list(scores,student,sc_log)
# search for class of mylist vector 
#check structure of mylist using str() function.
str(mylist)
[1] list of 3
[1] $:num [1:4] 20 30 40 50
[2] $:chr [1:4] "A""B""C""D"
[3] $:log [1:4] TRUE FALSE FALSE TRUE

which means list containing multiple data types like numeric, character and logical in mylist.But in vector there will be single data type of all elements in that vector

for ex:

for vector:

vector1 <- c(1,2,3,4)
Class(vector1)
[1] "Numeric"

#which means all elements of vector containing single data type that is numeric only.


标签: list r vector