knitr::opts_chunk$set(echo = TRUE)
R can denote:
Pros:
Cons:
We can divide language constructs into few categories:
There are 3 main value types:
Each variable is:
To assign a variable a = or <- operators are used (there is no difference between them).
To display variable simply type its name.
Examples:
# vector of numbers numbers = c(1, 2, 3) numbers # "single value" which in fact is also a vector (see below) singleNumber = 3 singleNumber # strings strings = c('hello', 'world') strings singleString = 'world' singleString # booleans bools = c(TRUE, FALSE) bools bool = TRUE bool # list myList = list(numbers, TRUE, list(1, 'a')) myList
Operators perform operations on whole vectors
num1 = 1:3 num2 = 5:7 num1 num2 num1 * num2
You can access given elements of a vector using their indexes.
You can also pass a vector of indexes.
numbers = c(1, 3, 5, 7) numbers[2] numbers[c(2, 4)]
And there is a useful shorthand syntax for creating range of numbers: from:to, e.g.:
numbers[2:4]
Each element of a vector can have a name.
Names can be assigned during vector creation or afterwords, using a names() function (which returns a vector of names):
# assigning names during vector creation # (2nd element will be unnamed) namedVector = c(name1 = 1, 2, name3 = 3) namedVector names(namedVector) names(namedVector)[2] = 'missingName' namedVector
If a vector has names, you can also access its elements by names:
namedVector = c(name1 = 1, name2 = 2, name3 = 3) namedVector[c('name1', 'name3')]
You can access given elements of a list in a same way then a vector. Returned object will be always a list.
Also the same rules apply to list's elements names.
myNamedList = list(name1 = 1:2, 5:6, name3 = c('a', 'b')) names(myNamedList) names(myNamedList) = c('n1', 'n2', 'n3') names(myNamedList) myNamedList[c(1, 3)] myNamedList['n2']
There are two special syntaxes to fetch single element of a list as a vector:
myList = list(a = 1:2, b = 3:4, c = 5:6) # with a [[ ]] myList[[2]] # with a $name myList$b
Matrix is a vector divided into columns.
You can create matrix using the matrix() function.
It takes a vector of values which will be splitted into columns, the number of rows and the number of columns.
myMatrix = matrix(1:12, nrow = 3, ncol = 4) myMatrix
You can split vector into rows instead of columns if you want:
myMatrix = matrix(1:12, nrow = 3, ncol = 4, byrow = TRUE) myMatrix
Matrix allow you to address elements using row and column indexes:
myMatrix = matrix(1:12, nrow = 3, ncol = 4) myMatrix[1:2, 3:4] myMatrix[1:2, ]
Instead of simply names matrix has colnames and rownames. Matrix name can be also assigned during matrix creation or afterwords.
myMatrix = matrix(1:12, nrow = 3, ncol = 4, dimnames = list(c('r1', 'r2', 'r3'), c('c1', 'c2', 'c3', 'c4'))) myMatrix colnames(myMatrix) = c('col1', 'col2', 'col3', 'col4') myMatrix
R is able to perform arithmetic operations on whole matrices:
matrix1 = matrix(1:4, nrow = 2, ncol = 2) matrix2 = matrix(5:8, nrow = 2, ncol = 2) matrix1 matrix2 matrix1 + matrix2
But matrix can be also threated as a normal vector:
myMatrix = matrix(1:12, nrow = 3, ncol = 4, dimnames = list(c('r1', 'r2', 'r3'), c('c1', 'c2', 'c3', 'c4'))) myMatrix myMatrix[c(1, 5, 7)]
Data frame is like a matrix applied to a list.
If all elements of a list have exactly the same length, we can treat it as a table displaying each element of a list as a table column.
You can convert a list to a data frame using as.data.frame() function or use a data.frame() shorthand:
myList = list('a' = 1:3, b = c('a', 'b', 'c')) myList as.data.frame(myList) data.frame('a' = 1:3, b = c('a', 'b', 'c'))
Similary to matrices, you can subset data frames using rows and columns indexes/names:
myDataFrame = data.frame('a' = 1:3, b = c('a', 'b', 'c'), c = 5:7) myDataFrame[2:3, c('a', 'c')] # be aware that if you select only one column, you will get not a data frame but a vector myDataFrame[2:3, c('a')]
And you can still access data frame's columns with the list's $columnName , [] and [[]] operators:
myDataFrame = data.frame('a' = 1:3, b = c('a', 'b', 'c'), c = 5:7) myDataFrame$b myDataFrame[2] myDataFrame[[2]]
There are to many to describe them briefly.
We will learn the ones important for us during workshop.
You can create your own functions using syntax:
myFunction = function(param1, param2){ result = param1 * param2 # any other code goes here... return(result) } myFunction(1:2, 3:4)
Of course your functions can take any number of parameters (also no one).
If you want your function to return nothing at all, use return(NULL) at the end.
Examples should be self descriptive:
if(1 == 2){ print('1 == 2') }else if(1 == 3){ print('1 == 3') }else{ print('else') } if(1 == 1){ print('1 == 1') }
And if you are familiar with a shorthand for if a then b else c avaiable in some languages (e.g. a ? b : c in C/C++/C#/Java or b if a else c in Python), you should welcome R's version:
ifelse(TRUE, 1, 2) ifelse(FALSE, 1, 2) # of course it works on vectors! ifelse(c(TRUE, FALSE), 1:2, 2:3)
There are two main loop types in R:
Examples
for(i in 1:10){ print(i) } i = 1 while(i <= 10){ print(i) i = i + 1 }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.