This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
#1a
a1 = c(5,"7",9,12,"15")
a2 = c(5,7,9,12,15)
typeof(a1)
## [1] "character"
typeof(a2)
## [1] "double"
#Vector a1 is a character vector, and a2 is a numeric vector.
#1b
#a1[1]+3
a2[1]+3
## [1] 8
#When the first function is run, the output gives me an error because a1 is a character so you can’t do arithmetic. The second function gives me 8 since adding 3 to element 1 which is 5 gives me 8. It works since the vector is numeric.
#1c
a1==a2
## [1] TRUE TRUE TRUE TRUE TRUE
identical(a1,a2)
## [1] FALSE
all.equal(a1,a2)
## [1] "Modes: character, numeric"
## [2] "target is character, current is numeric"
#When running the first line, its testing to see if all the elements in each vector are the same, regardless of whether or not the type of vectors are the same so they all are true. The second line compares the two vectors to see whether or not they are identical in type and comparing the elements. Since they are different types of vectors, then it said false. The third line tells me whether or not the two vectors are equal. It outputs their types.
#1d
a3=as.numeric(a1)
typeof(a3)
## [1] "double"
#A3 is a numeric vector.
a3==a2
## [1] TRUE TRUE TRUE TRUE TRUE
identical(a3,a2)
## [1] TRUE
all.equal(a3,a2)
## [1] TRUE
#When converting a1 into a3 as a numeric vector, it means that it is identical to a2 in the type of vector as well as the values of both.
#1e
a2[3]="9"
a2
## [1] "5" "7" "9" "12" "15"
typeof(a2)
## [1] "character"
#a2[1]+3
#When changing the 3rd value of a2 to a character, it converts all the values in the vector to characters making it a character vector. Adding 3 to the first value in the vector now gives an error.
#2a
?seq
## starting httpd help server ... done
b = (1:10)*2
b
## [1] 2 4 6 8 10 12 14 16 18 20
c = seq(1,10) * 2
c
## [1] 2 4 6 8 10 12 14 16 18 20
d = seq(1,13.3)*15
d
## [1] 15 30 45 60 75 90 105 120 135 150 165 180 195
e = seq(1,18)*11.1
e
## [1] 11.1 22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 111.0 122.1 133.2
## [13] 144.3 155.4 166.5 177.6 188.7 199.8
#2b
rep(1:10, times=5)
## [1] 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5
## [26] 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
rep(1:10, each=5)
## [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5
## [26] 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10
rep(1:10, length.out=50)
## [1] 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5
## [26] 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
#The three functions repeat the sequence of 1 to 10 in different ways. The first one repeats the sequence by going through each of the elements 5 times. The second one goes through each element five times before moving onto the next one. The third one repeats the sequence like the first one but stops once the length of the list is 50.
#3a
f=seq(1,10)*2 - 1
f
## [1] 1 3 5 7 9 11 13 15 17 19
?matrix
g=matrix(f, nrow = 2)
g
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 3 7 11 15 19
#3b
h=rbind(g, seq(1,5) * 2)
h
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 3 7 11 15 19
## [3,] 2 4 6 8 10
#3c
x = colSums(h)
x
## [1] 6 16 26 36 46
#3d
y = rowSums(h)
y
## [1] 45 55 30
#3e
i= h * rep(1,5)
i
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 3 7 11 15 19
## [3,] 2 4 6 8 10
#3f
is.matrix(y)
## [1] FALSE
is.vector(y)
## [1] TRUE
is.matrix(i)
## [1] TRUE
is.vector(i)
## [1] FALSE
#The result of part d was a vector while the result of part e is a matrix.