1. Matrices

  1. Construct a 5x4 matrix that is filled row-wise with the numbers 100-81, in that order. Store the matrix in a variable called MatA. Make sure you display your result so that you (and I) can see what you did in the final document.
numbers <-c(100:81)
MatA <- matrix(numbers, nrow = 5, ncol = 4)
MatA
##      [,1] [,2] [,3] [,4]
## [1,]  100   95   90   85
## [2,]   99   94   89   84
## [3,]   98   93   88   83
## [4,]   97   92   87   82
## [5,]   96   91   86   81
  1. Use single R commands to do each of the following.
  2. Display the 4th column of matA.
  1. Display matA with the 1st and 4th column deleted.
  2. Display matA with the 3rd row of mat A with the sequence 11-14.
  3. Display the transpose of matA. (Try using Stack Overflow or, more simply, Google to help you find the appropriate command in R).
  4. Replace the entry in the 2nd row and 3rd column with the number 1000. Display the result.
MatA[,4]
## [1] 85 84 83 82 81
MatA[,c(1, 3)]
##      [,1] [,2]
## [1,]  100   90
## [2,]   99   89
## [3,]   98   88
## [4,]   97   87
## [5,]   96   86
MatA[3,]<-c(11:14)
MatA
##      [,1] [,2] [,3] [,4]
## [1,]  100   95   90   85
## [2,]   99   94   89   84
## [3,]   11   12   13   14
## [4,]   97   92   87   82
## [5,]   96   91   86   81
numbers <-c(100:81)
MatA <- matrix(numbers, nrow = 5, ncol = 4)
t(MatA)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  100   99   98   97   96
## [2,]   95   94   93   92   91
## [3,]   90   89   88   87   86
## [4,]   85   84   83   82   81
MatA[2, 3]<- 1000
MatA
##      [,1] [,2] [,3] [,4]
## [1,]  100   95   90   85
## [2,]   99   94 1000   84
## [3,]   98   93   88   83
## [4,]   97   92   87   82
## [5,]   96   91   86   81
  1. Overwrite matA by removing the last column and replacing it with the last column sorted smallest to largest. Store the result in matB and display.
MatB<-MatA
MatB[,4]<-sort(MatB[,4])
MatB
##      [,1] [,2] [,3] [,4]
## [1,]  100   95   90   81
## [2,]   99   94 1000   82
## [3,]   98   93   88   83
## [4,]   97   92   87   84
## [5,]   96   91   86   85
  1. Dataframes.
  1. In class we created our own dataframe by using the ‘data.frame’ command. Typically we simply use a dataframe that has already been created for us. If the data is included in R, you can simply type the name of the dataset to use it. For example, the first 6 lines of the ‘iris’ dataset can displayed using the code:
head(iris, 6)

Change the above code to display the first 10 lines of the iris dataset. Also google (or guess) to find out how to display the last 10 lines of the dataset.

head(iris, 10)
tail(iris, 10)
  1. Change the names of the ‘iris’ dataset to be “Sepal Length” instead of “Sepal.Length” (and do this for the other columns as well). Display the first 3 lines of the resulting dataframe so you (and I) can see that it worked.

!!!!!!!!!!!!!!! i tried doing it broadly applicably like this:

namesRange <-c(1:4) for (eachIndex in namesRange){ currentName <- c(names(iris))[eachIndex] splitName <- c(strsplit(currentName, "“)) indexInQuestion <- which(splitName ==”.“) splitName[indexInQuestion] <-” "

}

I figured I’d just replace out each “.” with a " " I’m sure I’m just missing something.

!!!!!!!!!!!!!!!

irisNames <- c(names(iris))
irisNames[1] <- "Sepal Length"
irisNames[2] <- "Sepal Width"
irisNames[3] <- "Petal Length"
irisNames[4] <- "Petal Width"
irisNames
## [1] "Sepal Length" "Sepal Width"  "Petal Length" "Petal Width"  "Species"
names(iris) <- irisNames
names(iris)
## [1] "Sepal Length" "Sepal Width"  "Petal Length" "Petal Width"  "Species"
  1. Use the $ notation to extract the Petal Length column of iris. Then use ordinary vector indexing to extract 10th through 15th entries of that column. Repeat, but using a different method for extracting columns. (You only need to display the final step.)
petalLenghts <- iris$`Petal Length`
petalLenghts
##   [1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3 1.4
##  [19] 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6 1.6 1.5 1.5 1.4 1.5 1.2
##  [37] 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9 1.4 1.6 1.4 1.5 1.4 4.7 4.5 4.9 4.0
##  [55] 4.6 4.5 4.7 3.3 4.6 3.9 3.5 4.2 4.0 4.7 3.6 4.4 4.5 4.1 4.5 3.9 4.8 4.0
##  [73] 4.9 4.7 4.3 4.4 4.8 5.0 4.5 3.5 3.8 3.7 3.9 5.1 4.5 4.5 4.7 4.4 4.1 4.0
##  [91] 4.4 4.6 4.0 3.3 4.2 4.2 4.2 4.3 3.0 4.1 6.0 5.1 5.9 5.6 5.8 6.6 4.5 6.3
## [109] 5.8 6.1 5.1 5.3 5.5 5.0 5.1 5.3 5.5 6.7 6.9 5.0 5.7 4.9 6.7 4.9 5.7 6.0
## [127] 4.8 4.9 5.6 5.8 6.1 6.4 5.6 5.1 5.6 6.1 5.6 5.5 4.8 5.4 5.6 5.1 5.1 5.9
## [145] 5.7 5.2 5.0 5.2 5.4 5.1
petalLenghts[c(10:15)]
## [1] 1.5 1.5 1.6 1.4 1.1 1.2
iris$`Petal Length`[c(10:15)]
## [1] 1.5 1.5 1.6 1.4 1.1 1.2
  1. Look up (Google, or Stack Overflow. . .) the ‘unique’ command in R. Use it to determine how many unique species are represented in the iris dataset (I understand that you can just look at the dataset in this case-don’t do that this way.)
length(c(unique(iris$Species)))
## [1] 3
  1. What is the largest Petal Width in the data set? What is the smallest? Can you find an R command to determine which species these correspond to?
biggest<-max(iris$`Petal Width`)
smallest<-min(iris$`Petal Width`)
biggest
## [1] 2.5
smallest
## [1] 0.1
bigList <- c(which(iris$`Petal Width` == biggest))
smalList <- c(which(iris$`Petal Width` == smallest))
iris$Species[bigList]
## [1] virginica virginica virginica
## Levels: setosa versicolor virginica
iris$Species[smalList]
## [1] setosa setosa setosa setosa setosa
## Levels: setosa versicolor virginica
print("the widest petals belong to: ")
## [1] "the widest petals belong to: "
print(unique(iris$Species[bigList]))
## [1] virginica
## Levels: setosa versicolor virginica
print("the thinnest petals belong to: ")
## [1] "the thinnest petals belong to: "
print(unique(iris$Species[smalList]))
## [1] setosa
## Levels: setosa versicolor virginica
  1. Try using the ‘which’ command to determine the irises in this dataset which have a Petal Length larger than 5. Is there a command in R that will tell you which species this (largely) corresponds to?
longPetals <-c(which(iris$`Petal Length` > 5))
iris$Species[longPetals]
##  [1] versicolor virginica  virginica  virginica  virginica  virginica 
##  [7] virginica  virginica  virginica  virginica  virginica  virginica 
## [13] virginica  virginica  virginica  virginica  virginica  virginica 
## [19] virginica  virginica  virginica  virginica  virginica  virginica 
## [25] virginica  virginica  virginica  virginica  virginica  virginica 
## [31] virginica  virginica  virginica  virginica  virginica  virginica 
## [37] virginica  virginica  virginica  virginica  virginica  virginica 
## Levels: setosa versicolor virginica
unique(iris$Species[longPetals])
## [1] versicolor virginica 
## Levels: setosa versicolor virginica
  1. Finally, use an R command to find irises which have a petal length longer than 5 OR a petal length smaller than 2. Which species do these correspond to?
v1<- (c(which(iris$`Petal Length` < 2)))
v2<- (c(which(iris$`Petal Length` > 5)))
weirdVector <- c(v1, v2)
weirdVector
##  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19
## [20]  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38
## [39]  39  40  41  42  43  44  45  46  47  48  49  50  84 101 102 103 104 105 106
## [58] 108 109 110 111 112 113 115 116 117 118 119 121 123 125 126 129 130 131 132
## [77] 133 134 135 136 137 138 140 141 142 143 144 145 146 148 149 150
unique(iris$Species[weirdVector])
## [1] setosa     versicolor virginica 
## Levels: setosa versicolor virginica