R | Vectors vs List vs Array vs Matrix vs dataframe

For a non-R programmers some of the R data-types might be a bit confusing.


R
Non-R
InR, vector and list and arrays are different and has different properties.

List can be named or un-named

Un-named list are Arrays

Named lists are dictionary

However Array and Dictionary can contain another Array or a dictionary, but its still called Arrays and dictionaries.



Vector

  • Simplest form of R object
  • All values in a vector should be of same obj/class.
  • However they cant contain another vector or array or list.
  • (non-R programmers terms) its like array but with all values in the array having same data-types

List
  • It is like a vector but can contain any obj/class.
  • class also have named attributes.
  • can contain list within a list etc.
  • (non-R programmers terms) its like array (if not named) and dictionaries( if named)
Array
  • Its a list with one, two or more dimensions
  • dimensions can be named
  • (non-R programmers terms) its like array within another array
Matrix
  • Its more like an array but with only 2 dimensions
  • (non-R programmers terms) its still a matrix :)
DataFrame
  • data in table format, with rows and each column is an attribute.
  • when you read a csv file, you normally read that as dataFrames
  • (non-R programmers terms) Imagine table with rows and columns( with columnames)

Examples 

Vector
> x<- c(1:2, 1:3)
> class(x)
[1] "integer"
> x
[1] 1 2 1 2 3
> y<- c(1:2, 1:3)
> x<- c(1:2, 1:3,y)
> x
 [1] 1 2 1 2 3 1 2 1 2 3

List
> y<- c(1:2, 1:3)
> x<- list(1:2, 1:3,y)
> x
[[1]]
[1] 1 2

[[2]]
[1] 1 2 3

[[3]]
[1] 1 2 1 2 3

> x<- list(a=1:2, b=1:3, c=y)
> x
$a
[1] 1 2

$b
[1] 1 2 3

$c
[1] 1 2 1 2 3

> x$b
[1] 1 2 3

Array

> x <- array(1:20, dim=c(5,2,2), dimnames=list( NULL, c("ht", "wt"), c("male", "female")))
> x
, , male

     ht wt
[1,]  1  6
[2,]  2  7
[3,]  3  8
[4,]  4  9
[5,]  5 10

, , female

     ht wt
[1,] 11 16
[2,] 12 17
[3,] 13 18
[4,] 14 19
[5,] 15 20

Matrix
> x<- matrix(1:20, nrow=10, ncol=2)
> x
      [,1] [,2]
 [1,]    1   11
 [2,]    2   12
 [3,]    3   13
 [4,]    4   14
 [5,]    5   15
 [6,]    6   16
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20

DataFrames

df_data <- read.csv(file="/path/to/csvfilename.csv")

Comments

Ramesh said…
Thanks for this valuable blog. It was very informative and interesting. Keep sharing this kind of stuff.
Matlab in Chennai
HTML5 Training Institutes in Chennai
Matlab Training Center in Chennai
HTML Training in Chennai

Popular posts from this blog

Tableau - Accessing Tableau's DB

react-bootstrap-table | header column alignment fix

Tableau : Convert ESRI shapes into Tableau Format