THIS SOLUTION FILE IS A R MARKDOWN NOTEBOOK RATHER THAN AN R SCRIPT.
01-Getting Started This exercise is designed to get you familiar with our working environment for R and begin to use R. This exercise will be graded on a satisfactory/unsatisfactory basis and the expectation is that you make a reasonable attempt at completing the assignment.
The solutions will be posted after the due date. Examine the
solutions for any aspects of the exercise that you were not able to
complete.
You are allowed to use AI to help with understanding R coding but you
should not cut and paste from AI. You should use AI to help you
understand the solution and should type the code into R yourself.
01-Getting Started is due on 20 Jan at midnight. Download your R file from Posit Cloud and turn it in using the Assignment tool in Brightspace, which should appear in your calendar of upcoming due dates in the homepage for this class within Brightspace.
Below are a set of short exercises. These are intended to be simple exercises to allow you to become familiar with this learning environment before we move onto assignments from the textbook next week.
This flie is a R script, which is a plain text file. The hash on the left means that these lines are comments to be read by people and not the computer.
2+2
## [1] 4
the result ‘4’ should appear in the ‘Console’ window below.
rm(list = ls()) # Clear the workspace
myVector<-c(1,2,3,4,5,6)
# or
myVector<-1:6
as you did above in (1). To see the result, run the following line:
myVector
## [1] 1 2 3 4 5 6
You should see [1] 1 2 3 4 5 6 in the Console window below. You could also type myVector in the Console directly and then hit return.
Please continue with the exercises below on your own, attempt to complete items 3 to 17 below. Write your solutions in new lines beneath each item and save this file to download and turn in as described above. ################################################################################
myVector2<-myVector*2
4 Find the sum of the elements of myVector2.
sum(myVector2)
## [1] 42
myVector3<-myVector^2
plot(x=myVector, y=myVector3)
myVector3[myVector3<30]
## [1] 1 4 9 16 25
myMat<-matrix(1:12,nrow=3,ncol=4)
myMat[2,3]*myMat[3,2]
## [1] 48
rownames(myMat)<-c('a','b','c')
colnames(myMat)<-c('1','2','3','4')
myMat
## 1 2 3 4
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
OR
dimnames(myMat)<-list(c('a','b','c'),c('1','2','3','4'))
myMat
## 1 2 3 4
## a 1 4 7 10
## b 2 5 8 11
## c 3 6 9 12
my3d<-array(c(rep(1:12,3)),dim=c(3,4,3))
dimnames(my3d)<-list(c('a','b','c'),c('1','2','3','4'),
c('1d','2d','3d'))
my3d[2,,2]*my3d[3,3,3]
## 1 2 3 4
## 18 45 72 99
rep(1,10)
## [1] 1 1 1 1 1 1 1 1 1 1
1:100
## [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
OR
seq(from=1, to=100, by=1)
## [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
myDataFrame<-data.frame(myVector,myVector^2,myVector^3)
head(myDataFrame)
colnames(myDataFrame)<-c('Original', 'Modified', 'Squared')
head(myDataFrame)
plot(Original~Squared,data=myDataFrame)