Minor errors in R that can be disregarded

  • Matrix cannot store different type of variables

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    > # each element of x is an integer
    > x <- matrix(1:16,4,4)
    > str(x)
    int [1:4, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
    > # identify type of elements
    > typeof(x)
    [1] "integer"
    >
    > # each element of x is a character
    > x[,1] <- as.character(x[,1])
    > str(x)
    chr [1:4, 1:4] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16"
    > typeof(x)
    [1] "character"
    >
    > # each element of y is a list
    > y <- matrix(list("1",2,3,4),2,2)
    > str(y)
    List of 4
    $ : chr "1"
    $ : num 2
    $ : num 3
    $ : num 4
    - attr(*, "dim")= int [1:2] 2 2
    > typeof(y)
    [1] "list"
  • Mode VS Class

    Mode is an exclusive classification of objects based on their fundamental structure. The "atomic" modes consist of numeric, complex, character, and logical data types. Modes for recursive objects include 'list' and 'function,' among others. There is one and only one mode for an object.

    Class is an object's property that determines how generic functions interact with it. This is not a mutually exclusive category. By convention, if an object has no specific class assigned, such as a simple numeric vector, its class is the same as its mode.

    Changing the mode of an object is frequently referred to as "coercion." The mode of an object can change without requiring a class change. e.g.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    > x <- 1:16
    > mode(x)
    [1] "numeric"
    > dim(x) <- c(4,4)
    > x
    [,1] [,2] [,3] [,4]
    [1,] 1 5 9 13
    [2,] 2 6 10 14
    [3,] 3 7 11 15
    [4,] 4 8 12 16
    > class(x)
    [1] "matrix" "array"
    > is.numeric(x)
    [1] TRUE
    > mode(x) <- "character"
    > x
    [,1] [,2] [,3] [,4]
    [1,] "1" "5" "9" "13"
    [2,] "2" "6" "10" "14"
    [3,] "3" "7" "11" "15"
    [4,] "4" "8" "12" "16"
    > mode(x)
    [1] "character"
    > class(x)
    [1] "matrix" "array"
    >
    > x <- factor(x)
    > x
    [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Levels: 1 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9
    > class(x)
    [1] "factor"
    > mode(x)
    [1] "numeric"
  • NA in R

    Use NA as index will replace elements as NA

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    > x <- 1:16
    > x[NA]
    [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
    >
    > dim(x) <- c(4,4)
    > x[NA]
    [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
    >
    > x[NA,]
    [,1] [,2] [,3] [,4]
    [1,] NA NA NA NA
    [2,] NA NA NA NA
    [3,] NA NA NA NA
    [4,] NA NA NA NA

    R NA – What are \(\text{<Not Available>}\) Values?


To be continued …