To access R and R Studio which are installed on the Saint Ann’s server you can go to: http://rstudio.saintannsny.org:8787/ and log in with your Saint Ann’s email address.

(Note: You can install R https://cran.rstudio.com/ and R Studio https://www.rstudio.com/products/rstudio/download/ for free but you won’t need to since you can use the school version from anywhere with an internet connection.)

Once you log in, you will see four panes. For now, we will focus our attention on the console window in the lower left. Down the line you will likely find your self spending most of your time writing scripts in the upper left pane.

Computation

R can be used for computation, data manipulation, visualizations and simulations (among other things) but, mostly simply, R can be used as a calculator. Try typing the following into the console:

5+8
3*2
4^2
5/2
5 %/% 2
5 %% 2
5*(3+7)^2
sqrt(25)
25^(1/2)

R can also perform operations on vectors or matrices. Let’s try creating vectors, assigning them to variables and then performing calculations on them.

x <- 1:10
x
x+5
x^2

y <- seq(from=10,to=100,by=5)
y
1/y

z <- c(2,3,5)
z
10*z

mean(x)
x-mean(x)
(x-mean(x))^2
mean((x-mean(x))^2)
sqrt(mean((x-mean(x))^2))

Writing Functions

addthem <- function(x,y) {x+y}
addthem(x=3,y=5)
addthem(3,5)

addthem <- function(x=0,y=0, z=0) {x+y+z}
addthem(3,5,7)
addthem(3,5)
addthem(3)
addthem(1:4,10)

More things to try:

x <- c(20,25,23,27, 28)
x > 23
x >= 23
x == 20
7*(x >= 23)+3
length(x)
max(x)
min(x)
sum(x)
length(x) %% 2
(length(x) %% 2) == 0

Challenge 1: Try writing a function that computes the mean of a vector

x <- c(20,25,23,27, 28)

MEAN <- function(x,y){
#formula here
}

Challenge #2: Write a function that computes the root mean square error of a series of guess given the guesses and the true values. The following code may help getting you started. Note that the root mean square error should be 1.949 given the values provided below.

guesses <- c(20,25,23,27, 28)
values <- c(21, 23, 24, 25, 25)

RMSE <- function(x,y){
#formula here
}

Challenge 3: Try writing a function that computes the median of a vector

x <- c(20,25,23,27, 28)
y <- 1:8

MEDIAN <- function(x,y){
#formula here
}

Next up: Manipulating and Summarizing Data with dplyr and tidyr.