Homework 1 Solution

Jeremy King (334007221)

Q1

Part a

canada_rain_df = data.frame(read.table("/Users/Jeremy/downloads/rainfall_60-79.dat"))

Part b

nrow(canada_rain_df)
## [1] 4826
ncol(canada_rain_df)
## [1] 27

There are 4826 rows and 27 columns.

Part c

canada_rain_df[21,5]
## [1] 0

The value of canada_rain_df at row 21 column 5 is 0

Part d

canada_rain_df[3,]
##   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21
## 3 60  4  3  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0
##   V22 V23 V24 V25 V26 V27
## 3   0   0   0   0   0   0

Part e

names(canada_rain_df) <- c("year","month","day",seq(0,23))
names(canada_rain_df)
##  [1] "year"  "month" "day"   "0"     "1"     "2"     "3"     "4"     "5"    
## [10] "6"     "7"     "8"     "9"     "10"    "11"    "12"    "13"    "14"   
## [19] "15"    "16"    "17"    "18"    "19"    "20"    "21"    "22"    "23"

This function creates the labels for the columns of the data frame canada_rain_df. The 0-23 represent the hours of the day.

Part f

daily <- rowSums(canada_rain_df[ , (ncol(canada_rain_df) - 23):ncol(canada_rain_df)], na.rm = TRUE)
hist(daily, main = "Histogram of Daily Rainfall Amount Frequencies", xlab = "Rainfall")

For some reason there are daily values less than 0, so that is why it is messing up the histogram as there is no such thing as negative rainfall.

canada_rain_df[which.min(canada_rain_df$daily),]
##  [1] year  month day   0     1     2     3     4     5     6     7     8    
## [13] 9     10    11    12    13    14    15    16    17    18    19    20   
## [25] 21    22    23   
## <0 rows> (or 0-length row.names)
canada_rain_df$daily = NULL
canada_rain_df[canada_rain_df < 0] = NA
daily = rowSums(canada_rain_df[ , (ncol(canada_rain_df) - 23):ncol(canada_rain_df)], na.rm = TRUE)
hist(daily, main = "Histogram of Daily Rainfall Amount Frequencies", xlab = "Rainfall")

Q2

Part a

  1. vector1 <- c(5, 12, 7, 32) #creates a vector with four elements (5,12,7,32).
  2. max(vector1) #finds the max element of vector1, which is 32.
  3. sort(vector1) #sorts the elements of vector1 from least to greatest, making it (5,7,12,32).
  4. sum(vector1) #adds up all the elements of vector1 together and returns 56.

Part b

  1. vector2 <- c(“5”,7,12)

    #creates a vector with 3 elements (“5”,7,12).

  2. vector2[2] + vector2[3]

    #doesn’t run as intended as vector2 has non-numeric elements. Because one the elements was initialized as a string, the other numbers got converted to strings.

  3. dataframe3 <- data.frame(z1=“5”,z2=7,z3=12)

    #creates a data.frame with elements of (“5”,7,12)

  4. dataframe3[1,2] + dataframe3[1,3]

    #works as intended as the data.frame stores elements in their original form as they were when they were initialized. Thus, 7 and 12 are still numeric values.

  5. list4 <- list(z1=“6”, z2=42, z3=“49”, z4=126)

    #creates a list with elements 4 elements (“6”,42,“49”,126).

  6. list4[[2]]+list4[[4]]

    #works as intended as it calls the numeric value directly from list and returns 168

  7. list4[2]+list4[4]

    #doesn’t work as intended as list4[2] = z2, which is not a numerical value and thus cannot add them together.

Q3

Part a

seq(from = 1, to = 10000, by = 369)
##  [1]    1  370  739 1108 1477 1846 2215 2584 2953 3322 3691 4060 4429 4798 5167
## [16] 5536 5905 6274 6643 7012 7381 7750 8119 8488 8857 9226 9595 9964
seq(from = 1, to = 10000, length.out = 50)
##  [1]     1.0000   205.0612   409.1224   613.1837   817.2449  1021.3061
##  [7]  1225.3673  1429.4286  1633.4898  1837.5510  2041.6122  2245.6735
## [13]  2449.7347  2653.7959  2857.8571  3061.9184  3265.9796  3470.0408
## [19]  3674.1020  3878.1633  4082.2245  4286.2857  4490.3469  4694.4082
## [25]  4898.4694  5102.5306  5306.5918  5510.6531  5714.7143  5918.7755
## [31]  6122.8367  6326.8980  6530.9592  6735.0204  6939.0816  7143.1429
## [37]  7347.2041  7551.2653  7755.3265  7959.3878  8163.4490  8367.5102
## [43]  8571.5714  8775.6327  8979.6939  9183.7551  9387.8163  9591.8776
## [49]  9795.9388 10000.0000

Part b

rep(1:3, times = 3)
## [1] 1 2 3 1 2 3 1 2 3
rep(1:3, each = 3)
## [1] 1 1 1 2 2 2 3 3 3

As shown in the code, the first line goes from 1-3 3 times (1 2 3 1…) while the second line prints each element of 1-3 3 times (1 1 1 2…).