Abigail Lifrieri (932008788)

Q1

Part a

df <- read.table("rainfall_60-79.dat", header = FALSE)
canada_rain_df <- data.frame(df)

Part b

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

Part c

canada_rain_df[3,3]
## [1] 3

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))
head(canada_rain_df)
##   year month day 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## 1   60     4   1 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 2   60     4   2 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 3   60     4   3 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 4   60     4   4 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 5   60     4   5 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 6   60     4   6 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
tail(canada_rain_df)
##      year month day 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
## 4821   79    10  26 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0 12  0  0  0
## 4822   79    10  27 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 4823   79    10  28 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 4824   79    10  29 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 4825   79    10  30 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
## 4826   79    10  31 0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
##      23
## 4821  0
## 4822  0
## 4823  0
## 4824  0
## 4825  0
## 4826  0

“c” represents that we are making changes to our data frame. In this command, it is renaming our first three columns to “year”, “month” and “day”, and then renaming the next 24 columns the sequence of numbers from 0 to 23.

Part f

extract <- canada_rain_df[c(4 : 27)]
daily <- rowSums(extract)
newdf <- cbind(canada_rain_df, daily = c(daily))
hist(daily)

the problem here is that the x axis shouldn’t have negative values

Part g

removedaily <- newdf[c(28)]
replacenegatives <- replace(removedaily, removedaily<0, NA)
final_df_with_edited_daily <- cbind(canada_rain_df, daily = c(replacenegatives))
hist(final_df_with_edited_daily$daily)

Part h

str(final_df_with_edited_daily)
## 'data.frame':    4826 obs. of  28 variables:
##  $ year : int  60 60 60 60 60 60 60 60 60 60 ...
##  $ month: int  4 4 4 4 4 4 4 4 4 4 ...
##  $ day  : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ 0    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 1    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 2    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 3    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 4    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 5    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 6    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 7    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 8    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 9    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 10   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 11   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 12   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 13   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 14   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 15   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 16   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 17   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 18   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 19   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 20   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 21   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 22   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ 23   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ daily: num  0 0 0 0 0 0 0 0 0 0 ...

the type is “num”

Q2

Part a

vector1 <- c(6, 11, 8, 23)

This adds the values 6, 11, 8, 23 to a vector called “vector1”. This line is non-erroneous

max(vector1)
## [1] 23

This returns back the maximum value in vector1. This line is non-erroneous, as the max value is 23.

sort(vector1)
## [1]  6  8 11 23

This puts the contents of vector1 in default ascending order. This line is non-erroneous, as the sequence 6, 8, 11, 23 is correct.

sum(vector1)
## [1] 48

This sums together the contents of vector1, as it is a numeric vector. This line is non-erroneous, as the sum of 6+8+11+23 = 48.

Part b

vector2 <- c(11,"1",8)

This line creates a vector. Because there is one assigned character, the rest of the values become automatically set to characters also. This line is non-erroneous.

vector2[1] + vector2[3]
## Error in vector2[1] + vector2[3]: non-numeric argument to binary operator

This is erroneous, because you cannot add non-numerical values.

dataframe3 <- data.frame(z1="6",z2=11,z3=8)

This creates a data frame called “dataframe3”. This is non-erroneous.

dataframe3[1,2] + dataframe3[1,3]
## [1] 19

This adds components 11 and 8 together. This adds two numeric components, so it is non-erroneous.

list4 <- list(z1="6", z2=11, z3="8", z4=23)

This creates a list called “list4” that contains two numbers (11, 23) and two characters (6, 8). This line is non-erroneous.

list4[[2]]+list4[[4]]
## [1] 34

This adds list4 components 2 and 4 together, which is 11+23=34. This line is non-erroneous.

list4[2]+list4[4]
## Error in list4[2] + list4[4]: non-numeric argument to binary operator

This is erroneous, because reading into a list with [] does not drop names or structures, so you cannot use + like you could if you used [[]].

Q3

Part a

seq(1, 10000, 365)
##  [1]    1  366  731 1096 1461 1826 2191 2556 2921 3286 3651 4016 4381 4746 5111
## [16] 5476 5841 6206 6571 6936 7301 7666 8031 8396 8761 9126 9491 9856
seq(1, 10000, length.out = 100)
##   [1]     1   102   203   304   405   506   607   708   809   910  1011  1112
##  [13]  1213  1314  1415  1516  1617  1718  1819  1920  2021  2122  2223  2324
##  [25]  2425  2526  2627  2728  2829  2930  3031  3132  3233  3334  3435  3536
##  [37]  3637  3738  3839  3940  4041  4142  4243  4344  4445  4546  4647  4748
##  [49]  4849  4950  5051  5152  5253  5354  5455  5556  5657  5758  5859  5960
##  [61]  6061  6162  6263  6364  6465  6566  6667  6768  6869  6970  7071  7172
##  [73]  7273  7374  7475  7576  7677  7778  7879  7980  8081  8182  8283  8384
##  [85]  8485  8586  8687  8788  8889  8990  9091  9192  9293  9394  9495  9596
##  [97]  9697  9798  9899 10000

Part b

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

The difference between these two lines is that the first repeats the whole vector (in this case (1, 2, 3)) five times, while the second line repeats each COMPONENT five times before changing to the next.