October 11th, 2016

Basic objects in R

Vectors

  • What is a vector?
  • What basic types of data does a vector handle?
  • How do we create a vector?
  • How do we assign a vector to a variable?
  • How do we put names to a vector?
  • How do we index a vector?

Vectors in R

  • All elements of the vector must have the same type
  • Basic types are
    • Numeric
    • Logic
    • Character
    • Factor

Describe each type

Creating vectors

  • Simple concatenation
    • c(1,2,3)
    • c(TRUE, TRUE, FALSE, TRUE)
    • c("alpha", 'beta', "gamma")
  • A comparison creates a logical vector
    • weight > 25
  • Any character vector can be transformed into a factor
    • factor(c("head","tail"))
    • factor(rep("male",6), levels=c("male","female"))

Sequences & Repetitions

  • Sequences
    • 4:9
    • seq(4,10,2)
  • Repetitions
    • rep(1,3)
    • rep(c(TRUE,FALSE),3)
    • rep(c(TRUE,FALSE),c(3,3))
  • Missing data
    • c(NA,TRUE, FALSE)

Exercise

  • How do we write " inside a character string?
  • How do we write a TAB?
  • How do we assign a value to a variable?
  • What characters can be used to name a variable?

Answers

Special characters are coded with two symbols: \", \\, \n, \t.

We use <- for assignment.

Variable names start with a letter, and are followd by a letter, number or dot.

Matrices

Example of Matrix

> state.x77
               Population Income Illiteracy Life Exp Murder HS Grad Frost
Alabama              3615   3624        2.1    69.05   15.1    41.3    20
Alaska                365   6315        1.5    69.31   11.3    66.7   152
Arizona              2212   4530        1.8    70.55    7.8    58.1    15
Arkansas             2110   3378        1.9    70.66   10.1    39.9    65
California          21198   5114        1.1    71.71   10.3    62.6    20
Colorado             2541   4884        0.7    72.06    6.8    63.9   166
Connecticut          3100   5348        1.1    72.48    3.1    56.0   139
Delaware              579   4809        0.9    70.06    6.2    54.6   103
Florida              8277   4815        1.3    70.66   10.7    52.6    11
Georgia              4931   4091        2.0    68.54   13.9    40.6    60
Hawaii                868   4963        1.9    73.60    6.2    61.9     0
Idaho                 813   4119        0.6    71.87    5.3    59.5   126
Illinois            11197   5107        0.9    70.14   10.3    52.6   127
Indiana              5313   4458        0.7    70.88    7.1    52.9   122
Iowa                 2861   4628        0.5    72.56    2.3    59.0   140
Kansas               2280   4669        0.6    72.58    4.5    59.9   114
Kentucky             3387   3712        1.6    70.10   10.6    38.5    95
Louisiana            3806   3545        2.8    68.76   13.2    42.2    12
Maine                1058   3694        0.7    70.39    2.7    54.7   161
Maryland             4122   5299        0.9    70.22    8.5    52.3   101
Massachusetts        5814   4755        1.1    71.83    3.3    58.5   103
Michigan             9111   4751        0.9    70.63   11.1    52.8   125
Minnesota            3921   4675        0.6    72.96    2.3    57.6   160
Mississippi          2341   3098        2.4    68.09   12.5    41.0    50
Missouri             4767   4254        0.8    70.69    9.3    48.8   108
Montana               746   4347        0.6    70.56    5.0    59.2   155
Nebraska             1544   4508        0.6    72.60    2.9    59.3   139
Nevada                590   5149        0.5    69.03   11.5    65.2   188
New Hampshire         812   4281        0.7    71.23    3.3    57.6   174
New Jersey           7333   5237        1.1    70.93    5.2    52.5   115
New Mexico           1144   3601        2.2    70.32    9.7    55.2   120
New York            18076   4903        1.4    70.55   10.9    52.7    82
North Carolina       5441   3875        1.8    69.21   11.1    38.5    80
North Dakota          637   5087        0.8    72.78    1.4    50.3   186
Ohio                10735   4561        0.8    70.82    7.4    53.2   124
Oklahoma             2715   3983        1.1    71.42    6.4    51.6    82
Oregon               2284   4660        0.6    72.13    4.2    60.0    44
Pennsylvania        11860   4449        1.0    70.43    6.1    50.2   126
Rhode Island          931   4558        1.3    71.90    2.4    46.4   127
South Carolina       2816   3635        2.3    67.96   11.6    37.8    65
South Dakota          681   4167        0.5    72.08    1.7    53.3   172
Tennessee            4173   3821        1.7    70.11   11.0    41.8    70
Texas               12237   4188        2.2    70.90   12.2    47.4    35
Utah                 1203   4022        0.6    72.90    4.5    67.3   137
Vermont               472   3907        0.6    71.64    5.5    57.1   168
Virginia             4981   4701        1.4    70.08    9.5    47.8    85
Washington           3559   4864        0.6    71.72    4.3    63.5    32
West Virginia        1799   3617        1.4    69.48    6.7    41.6   100
Wisconsin            4589   4468        0.7    72.48    3.0    54.5   149
Wyoming               376   4566        0.6    70.29    6.9    62.9   173
                 Area
Alabama         50708
Alaska         566432
Arizona        113417
Arkansas        51945
California     156361
Colorado       103766
Connecticut      4862
Delaware         1982
Florida         54090
Georgia         58073
Hawaii           6425
Idaho           82677
Illinois        55748
Indiana         36097
Iowa            55941
Kansas          81787
Kentucky        39650
Louisiana       44930
Maine           30920
Maryland         9891
Massachusetts    7826
Michigan        56817
Minnesota       79289
Mississippi     47296
Missouri        68995
Montana        145587
Nebraska        76483
Nevada         109889
New Hampshire    9027
New Jersey       7521
New Mexico     121412
New York        47831
North Carolina  48798
North Dakota    69273
Ohio            40975
Oklahoma        68782
Oregon          96184
Pennsylvania    44966
Rhode Island     1049
South Carolina  30225
South Dakota    75955
Tennessee       41328
Texas          262134
Utah            82096
Vermont          9267
Virginia        39780
Washington      66570
West Virginia   24070
Wisconsin       54464
Wyoming         97203

Matrices

Like vectors but in 2 dimensions

> weight
[1] 60 72 57 90 95 72
> matrix(weight, nrow=2, ncol=3)
     [,1] [,2] [,3]
[1,]   60   57   95
[2,]   72   90   72

Values go column-by-column

Matrices

We can ask to fill it row-by-row

> weight
[1] 60 72 57 90 95 72
> matrix(weight, nrow=2, ncol=3, byrow=T)
     [,1] [,2] [,3]
[1,]   60   72   57
[2,]   90   95   72

Matrix dimensions

> M <- matrix(weight, nrow=2, ncol=3)
> dim(M)
[1] 2 3
> nrow(M)
[1] 2
> ncol(M)
[1] 3

Rows and columns names

> colnames(M) <- c("A","B","C")
> rownames(M) <- c("x","y")
> M
   A  B  C
x 60 57 95
y 72 90 72

Indexing Matrices

  • Objects of type matrix or array use an index for each dimension
  • If an index is omitted, all the range is returned
> M[2, ]
 A  B  C 
72 90 72 
> M[ ,3]
 x  y 
95 72 

Indexing Matrices

Notice that sometimes the answer is a vector, other times is a matrix

> M[ ,2:3]
   B  C
x 57 95
y 90 72
> M[ ,3]
 x  y 
95 72 

Keeping matrices as matrices

To avoid a matrix column becoming a vector, use drop=FALSE

> M[ ,3]
 x  y 
95 72 
> M[ ,3, drop=FALSE]
   C
x 95
y 72

Exercise

Make a copy of the matrix state.x77 into M

> M <- head(state.x77)
> M
           Population Income Illiteracy Life Exp Murder HS Grad Frost
Alabama          3615   3624        2.1    69.05   15.1    41.3    20
Alaska            365   6315        1.5    69.31   11.3    66.7   152
Arizona          2212   4530        1.8    70.55    7.8    58.1    15
Arkansas         2110   3378        1.9    70.66   10.1    39.9    65
California      21198   5114        1.1    71.71   10.3    62.6    20
Colorado         2541   4884        0.7    72.06    6.8    63.9   166
             Area
Alabama     50708
Alaska     566432
Arizona    113417
Arkansas    51945
California 156361
Colorado   103766

Exercises

  • Find the value of the third row, fourth column
  • Get a vector with the fifth column
  • What is the difference between the first and the second row
  • What is the Illiteracy at Colorado?
    • change it to 0
  • Change the names of the columns to Turkish
  • Change the names of rows to abbreviations
    • Hint: state.abb