Problem Set 5 | 6 May 2020 - 13 May 2020 at 10 AM

Answer the following conceptual questions.

(i) Other than its contents, what are the three properties of a vector?

The three properties of a vector are type, length, and attributes.

temp <- c(1,2,3,4,5)
print(typeof(temp))
## [1] "double"
print(length(temp))
## [1] 5
print(attributes(temp))
## NULL

(ii) List the four common types of atomic vectors. What are the two rare types?

The four common types of atomic vector are logical, integer, double (sometimes called numeric), and character. The two rarer types are complex and raw.

logical <- c(TRUE, FALSE, TRUE)
print(typeof(logical))
## [1] "logical"
# L is a suffix to make a number explicitly and integer. This has a more limited number range compared to doubles
integer <- c(1L,2L,3L)
print(typeof(integer))
## [1] "integer"
double <- c(1.1,2.2,3.3)
print(typeof(double))
## [1] "double"
character <- c("one", "two", "three")
print(typeof(character))
## [1] "character"

(iii) Describe the difference between an if-if statement and an else if statement and code an example that highlights the diffence.

if-if tests all conditions, whereas the second else if tests only as many as needed. For else if, if it finds one condition that is True , it stops and doesn’t evaluate the rest. The else if statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

############## If-If ############
x <- c("an", "a", "s", "t", "as", "i", "a")
for (i in seq(1:length(x))){
  if (length(x[i]) >=  1) {
    print(x[i])
  }
  if (length(x[i]) <  3) {
    print(x[i])
  }
}
## [1] "an"
## [1] "an"
## [1] "a"
## [1] "a"
## [1] "s"
## [1] "s"
## [1] "t"
## [1] "t"
## [1] "as"
## [1] "as"
## [1] "i"
## [1] "i"
## [1] "a"
## [1] "a"
############## If-Else if ############
for (i in seq(1:length(x))){
  if (length(x[i]) >=  1) {
    print(x[i])
  }
  else if (length(x[i]) <  3) {
    print(x[i])
  }
}
## [1] "an"
## [1] "a"
## [1] "s"
## [1] "t"
## [1] "as"
## [1] "i"
## [1] "a"
# The If-Else statement recreates/reprints the name while, the If-If statement is redundant.

(iv) What are attributes? How do you get them and set them? Code an example.

Attributes are any kind of information (e.g. additional metadata) that is assigned to an element of an object, and you can use them yourself to add information to any object. You can get them using the attribute() function and set them using the attr() function. The attr() function takes two important arguments. attr(object, name of attribute want to set/change).

Note: Objects in R can have many properties associated with them, called attributes. These properties explain what an object represents and how it should be interpreted by R. Quite often, the only difference between two similar objects is that they have different attributes. Some important attributes are listed below. Many objects in R are used to represent numerical data—in particular, arrays, matrices, and data frames. So many common attributes refer to properties of these objects.

  • class - the calss of the object
  • comment - a comment on the object; often a description of what the object means
  • dim - dimensions of the object
  • dimnames - names associated with each dimension of the object
  • names - names of an object. Depends on object type, e.g. it returns the name of each data column in a data frame
  • row.names - the name of each row in an object
  • tsp - start time for an object. Useful for time series data.
  • levels - levels of a factor
# Example 1: adding a new attribute
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
attributes(iris)
## $names
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     
## 
## $class
## [1] "data.frame"
## 
## $row.names
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## [145] 145 146 147 148 149 150
attr(iris, "Species") <- "There are three: setosa, virginica, and versicolor"
attributes(iris)
## $names
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     
## 
## $class
## [1] "data.frame"
## 
## $row.names
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## [145] 145 146 147 148 149 150
## 
## $Species
## [1] "There are three: setosa, virginica, and versicolor"
# Example 2: Renaming column names 
head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
attributes(cars)
## $names
## [1] "speed" "dist" 
## 
## $class
## [1] "data.frame"
## 
## $row.names
##  [1]  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] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
attr(x = cars, which = "names") <- c("aaa", "bbb")  
attributes(cars)
## $names
## [1] "aaa" "bbb"
## 
## $class
## [1] "data.frame"
## 
## $row.names
##  [1]  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] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

(v) Describe all the possible differences between a list and an atomic vector and between a matrix and a dataframe.

The elements of a list can be any type (even a list); the elements of an atomic vector are all of the same type. You can’t perform mathematical functions on a list, but you can on a vector. You can have a list of lists, you cannot have a vector of vectors.

Every element of a matrix must be the same type; in a data frame, the different columns can have different types.

(vi) Can you have a list that is a matrix? Can a data frame have a column that is a matrix? No, but You can make a “list-array” by assigning dimensions to a list.

Yes, you can make a matrix a column of a data frame with df$x <- matrix(), or using I() when creating a new data frame data.frame(x = I(matrix())).

?I() # Inhibit Interpretation/Conversion of Objects

# In function data.frame. Protecting an object by enclosing it in I() in a call to data.frame inhibits the conversion of character vectors to factors and the dropping of names, and ensures that matrices are inserted as single columns. I() can also be used to protect objects which are to be added to a data frame, or converted to a data frame via as.data.frame.

m <- matrix(2,2)
data.frame(I(m)) # It achieves this by prepending the class "AsIs" to the object's classes. Class "AsIs" has a few of its own methods, including for [, as.data.frame, print and format.
##   m
## 1 2
## 2 2
data.frame(m)
##   m
## 1 2
## 2 2

(vii) Can you take the sum() of a list? Can you have a 2-D vector (aka. a vector of vectors)?

No, you can’t perform mathematical operations on a list. And no vectors are only 1D.

(viii) How many data structures does R language have? What’s the difference between them?

5 types: Homogenous vs. Heterogenous

##    Homogeneous     Heterogeneous
## 1d "Atomic vector" "List"       
## 2d "Matrix"        "Data frame" 
## nd "Array"         " "

(ix) Let’s say you have some dataframe, d. What is d[1,]? What is d[, 5].

d[1,] gives you all the columns in row 1, and d[,5] gives you all the rows in column 5.

(x) If you had a list of lists, ll, how would you get 1 element in the 4th column and 3rd row.

# ll[[3]][4] because the set up is ll[[row]][column]