To begin, we will want to install R. Go to cran.r-project.org to download the correct version for you OS.
After thats been downloaded, we will want to install RStudio. www.rstudio.com to get the correct version.
How do we print a “Hello World!” message?
print("Hello World!")
Try printing your name instead.
print("Addison")
What about the following code. What do you think it is doing?
a <- 15
print(a)
Why do we not need parenthesis in the above print statement?
In this case, “a” is a variable. We assigned it the value of 15 using the “<-” operator.
Variable names can be as long or short. You can use upper case and lower case letters, as well as numbers and special characters like “_" and “.”. But, variable names MUST start with a letter.
It is good to name a variable something with meaning. Say I am taking the mean of everyone’s ages, I might call the varialbe “mean_age”.
For the next couple chuncks of code, read the code and discuss with a neighbor. What do you think the output will be? Then run the code and check! If the output is something you did not expect, why? Make sure you and your neighbor understand before moving on. Try making small changes to the code. Are you seeing the changes you expect?
b <- 12.75
print(b)
Just because we have given b a numeric value, does not mean that we can overwrite b with a different type.
b <- 12.75
print(b)
b <- "This is some text."
print(b)
b <- TRUE
print(b)
We can do basic math operations.
a <- 10
b <- 15
x <- a + b
print(x)
x <- a - b
print(x)
x <- a * b
print(x)
x <- a / b
print(x)
x <- a^2
print(x)
x <- a^4
print(x)
x <- b^2
print(x)
R can handle logical operators as well.
x <- 2
y <- 3
z <- 2
a <- x == y
print(a)
a <- x == z
print(a)
z <- y > x
print(a)
a <- x != y
print(a)
What about the following code? What do you think is happening?
a = 10
x = a^0.5 # + 3
print(x)
The “#” is a comment. It causes the rest of the line of code to be ignored.