For example, a vector may contain all numeric data or all character data.
A list is a special type of object that can contain data of multiple types.
Lists are characterized by the fact that their element do not need to be of the same object type.
Lists can contain elements of different types so that the list elements may have different modes.
Lists can even contain other structured objects, such as lists and data frames which allows to create recursive data structures.
Lists can be indexed by position.
So x [ [5] ] refers to the fifth element of x.
Lists can extract sublists.
- So x [c (2,5) ] is a sublists of x that consists of the second and fifth elements.
Lists elements can have names.
- Both x [ ["Students"] ] and x$Students refer to the element named "Students".
Difference between a vector and a list :
- In a vector, all element must have the same mode.
- In a list, the elements can have different modes.
Modes :
Every objects has a mode.
The mode indicates how the object is stored in memory: as a
- number
- character string
- list of pointers to other objects,
- function etc.
Mode function give us such information.
Syntax
mode ( )
Example
> mode (1.234)
[1] "numeric"
> mode ( c(5,6,7,8) )
[1] "numeric"
> mode ("India")
[1] "character"
> mode ( c( "India" , "USA") )
[1] "character"
Example
> mode (factor (c ("UP" , "MP") ) )
[1] "numeric "
> mode (list ("India", "USA") )
[1] "list"
>mode (data.frame (x=1:2, Y=c ("India", "USA" ) ) )
[1] "list"
> mode (print)
[1] "function"