Welcome to one of our Crash Course series! In this post we will cover R. This course is meant for coder who have already learned a programming language, but feel free to try if you are up for the challenge.
The idea here is there are some items common to almost every language:
You could also argue classes, but there are many functional first programming languages, so we will keep with this list. After this list, you start to get into more specific language features, like modeling in R, as well as more intermediate and advance features like the standard library and concurrency.
We will briefly cover each of the above topics before using R. Let's get started!
Let's start with the simple hello world script. We will introduce the print
function
which is common to many languages.
print("Hello World")
In R, we can declare variables using the <-
or =
assignment operator. You do not need to specify a type. I personally prefer the =
since it is one less character to type, but I will use <-
as many R programmers still use this operators.
name <- "Keith"
print(name)
Okay, not to get so more details going. Let's briefly look at each of the basic data type in R. You should have seen many of these in your previous language. However, because R was meant for Mathematics, there are a few special types.
## Booleans or Logical
v <- TRUE
print(v)
## Strings or Character
text <- "Hello, Everoyne!"
print(text)
## Numbers or Numeric
age <- 18
print(age)
amount <- 12.9
print(amount)
Vectors are one of the mathematic types in R. However, the operate much like Lists yet all values must be the same type.
colors <- c('red', 'green', 'blue')
print(colors)
samples <- c(20, 34, 3)
print(samples)
Lists similar to arrays and lists in other languages. They allow you to hold many different types.
@TDOD. Fill in the non creating
## Creating
todo <- c("step1", "step2")
mixedList <- c(1, 2, "one", "two")
print(todo)
print(mixedList)
## Acess/reading
print(todo[2])
## Updating
mixedList[1] = "a new item"
print(mixedList[1])
## Deleting
del todo[1]
print(todo)
## Helpful functions
print(len(todo))
combined = todo + mixedList
print(combined)
repeated = [4] * 12
print(repeated)
You can use lists to create a list of list, but R offers the matrix type for that is helpful. Again, this is because of the math based design. They are limited to two dimensions.
mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow=3, ncol=3, byrow=TRUE)
print(mat)
Arrays are similar to the list above, but have helpful properties of matrices. They extend matrices and allow for multiple dimensions.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
Factors are another special R type. They are used for modeling and plotting mostly. They are simply a vector but with the unique named values.
colors <- c("red", "red", "green", "blue")
colors.factor <- factor(colors)
print(colors.factor)
print(nlevels(colors.factor))
The final type we will look at are called Data Frames. These are similar to dictionaries in other languages, but have many special properties when plotting and modeling.
BMI <- data.frame(
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
The basic operators are the same in R as most languages. However, since we have more data types, lect the vector, we can do some extra mathematical operations, like Linear Algebra. This can be programmed in other languages, but R has these built in. Let's take a review of the most common.
first = c(2, 3, 4)
second = c(1, 2, 3)
## Math
print(first + second)
print(first - second)
print(first * second)
print(first )/ second)
## Comparisons
# equal to
print(first == second)
# no equal
print(first != second)
# greater than
print(first > second)
# greater than or equal
print(first >= second)
# less than
print(first < second)
# less than or equal
print(first <= second)
For R, if statements are essential the same as general programming languages. Let's see how this works for conditional flows (if, else if, and else).
age = 20
if (age > 60) {
print("You are to old!")
} else if (age < 5) {
print("You are to young!")
} else {
print("Just right")
}
R has both a for and while loop. One special keyword in R is the in
keyword, which let's you use generators
. This is very helpful when iterating of lists or vectors, but has many other advantages.
todoList <- c("one", "two", "three")
for (item in todoList) {
print(item)
}
To create functions in R, we use the function
keyword followed by parenthesis and the arguments needed. In R, we muse assign a function to a variable. Also, there is no return keyword. The last line of the function is returned.
getFullName <- function (firstName, lastName) {
firstName + " " + lastName
}
full <- getFullName("Keith", "Holliday")
print(full)