Introduction to R for Data Science (Part One)
The first introduction to R for data science. This will cover arithmetic, variables, vector basics, and more.
Setting up RStudio (Windows)
Here are the steps for setting up R:
Go to Posit and download RStudio Desktop Open Source Edition (should be the free version)
Download R by clicking on it. It should take you to a page where you can select “Download R for Windows”. Once you have clicked on it, select “base” and then it will take you to a page where you can download R for windows. After that, the installation will begin.
Once you have completed the installation for R, download RStudio
Once you have completed the installation for RStudio, you should be done
R Basics
Arithmetic
Arithmetic is pretty simple in R. You would write out what kind of calculation you want to perform in the Console.
So the calculation I did was:
1 + 2 which gave me 3
100 + 11 which gave me 111
5 - 3 which gave me 2
1 / 2 which gave me 0.5
5 %% 2 which gave me 1 (since 1 is the remainder of how many times 2 can go into 5)
Variables
A hashtag (#) is how you would write comments.
In order to assign a value to a name we would use <-.
So in this case I assign 100 to the variable. Variable now equals to 100, so when I call variable again, it gives me 100. You can name it however you want.
I also assigned 1000 to the bank, so when I call the bank again, it gave me 1000. You can see the values I assign on the “Environment” to the right.
You can also write bank as:
bank.account <- 1000
bankAccount <- 1000
bank_account <- 1000
It will all work.
This is how you would add the values together. I have assigned 50 to deposit and then add the deposit to the bank. This would give me 1050 as bank was 1000, so 1000 + 50 = 1050.
Note that if you do this the new value for bank is 1050, it isn’t 1000 anymore.
Basic Data Types
Numeric data types consist of decimals known as floating point values and whole numbers.
So I can assign something like:
a <- 2.2
Whole numbers are also part of numeric such as 5, 6, and 12.
Logical class consists of boolean values which are either true or false.
So in R, it is written as:
TRUE
FALSE
T
F
Example of assigning true and false:
a <- T
a <- F
T is True and F is False
Characters are strings, but in R we call them characters.
It is written as:
“Hello”
‘Hello’
Example of assigning characters:
a <- “Hello”
b <- “Hello”
Single quotes and double quotes produce the same outcome, but I recommend using double quotes.
Class function is what you would use to know what data type it was.
It is written as:
class()
In a nutshell, this is what it is.
Vector Basics
A vector is a one-dimensional array that can hold numeric, logical, or character data elements.
This is what a vector is. You would use c in order to create a one-dimensional array as you can see in the picture above. You can assign whatever numeric, characters you want, I have just chosen these as an example.
One of the rules of a vector is that you can’t mix data types into the element of that specific vector.
For example, you can’t do:
v <- c(TRUE, 534, 23)
v <- c(‘NUM’, 23, 42)
Just can’t combine data types.
You can add characters to numeric in a case like:
Note that you must use the names function. You can’t say things like:
nan(temp) <- c(‘Mon’, ‘Tue’, ‘Wed’)
noooo(temp) <- c(‘Mon’, ‘Tue’, ‘Wed’)
It has to be:
names(temp) <- c(‘Mon’, ‘Tue’, ‘Wed’)
for it to be correct.
You normally don’t want to write like that. You want to write it like this:
I assign the characters to days and then just added days to temps.
Vector Operations
You can work with vectors like:
Here are some functions that R has:
You can use the sum function to add the arrays together for that specific name that you have chosen.
There are also mean, standard deviation, maximum, and minimum.
You can also use a product function to multiply each number:
So in this case, 1 * 2 * 3 = 6 and 5 * 6 * 7 = 210.
Comparison Operators
We would use comparison operators to compare variables that return logical values.
Remember spacing plays a role in v < -1. If you were to write v <- 1 then you’re assigning v to -1, so remember the spacing.
We can use vectors with comparison operators like:
As a reminder make sure to double-check if you’re doing a comparison operator with a negative number or if you’re assigning a negative number.
[End of Part One]