7.4 Exercice 1. Getting started ~ 10 minutes
Create the script “exercise1.R” (in R Studio: File -> New File) and save it to the “Rcourse/Module1” directory: you will save all the commands of exercise 1 in that script.
Remember you can comment the code using #.
1- From the console, go to Rcourse/Module1. First check where you currently are with getwd(); then go to Rcourse/Module1 with setwd(). Remember that ~ is a shortcut to your home directory.
Answer
getwd()
setwd("Rcourse/Module1")
setwd("~/Rcourse/Module1")2- Using R as a calculator, calculate the square root of 654 (don’t know the function? Search the internet!).
Answer
sqrt(654)## [1] 25.57342
3- Create a new object “myobject” that contains value 60. Show “myobject” in the console.
Answer
myobject <- 60
myobject## [1] 60
4- Subtract 1 from myobject. Reassign.
Answer
myobject <- myobject - 15- Create a new object “mysqrt” that will store the square root of “myobject.”
Answer
mysqrt <- sqrt(myobject)6- Create a new object “mydiv” that will store the result of “myobject” divided by “mysqrt.”
Answer
mydiv <- myobject / mysqrt