This is an example that you can either use rep function or for loop to achieve same thing. But the rep is logical, shorter and nicer :)
In [6]:
vec_old = rep(c("a","b"),5)
vec = c("a", "b")
vec_new = c()
up_to = 5
for (i in 1:up_to){
vec_new = c(vec_new, vec)
}
vec_new == vec_old
In [1]:
In [3]:
seq(1, 20, by = 3)
In [2]:
rep(c(1:5), 3)
In [4]:
sqrt(16)
In [5]:
num_vec = 1:25
In [6]:
length(num_vec)
In [7]:
sum(num_vec)
In [8]:
mean(num_vec)
In [9]:
min(num_vec)
In [10]:
which(num_vec > 15)
Packages are installed like this:
In [ ]:
install.packages("ggplot2")
pack = c("dplyr", "ezanova")
install.packages(pack)
When installed, you need to allow them in each project/R instance. Loaded packages are vaailable to all scripts in the R session.
In [ ]:
library(dplyr)
library(ggplot)
Functions are defined as:
In [ ]:
fun = function(argument1, argument2){
#does something
return("SUCESS")
}
For example, if you are working with a lots of triangles you can come to the following situation:
In [ ]:
sideA1 = 3
sideA2 = 4
sideA3 = sqrt((sideA1 ^ 2) + (sideA2 ^ 2))
sideB1 = 10
sideB2 = 15
sideB3 = sqrt((sideB1 ^ 2) + (sideB2 ^ 2))
it would work, but it looks like crap. We can do several things about it. Firstly, lets put triangles to vectors to keep it neat:
In [7]:
triangle1 = c(3, 4, NA)
triangle2 = c(10, 15, NA)
triangle1[3] = sqrt((triangle1[1] ^ 2) + (triangle1[2] ^ 2))
triangle2[3] = sqrt((triangle2[1] ^ 2) + (triangle1[2] ^ 2))
Nicer, but still looks kinda crap. Lets fix it with a function.
In [ ]:
# calculate trialngle hypotenuse
hypotenuse = function(sideA, sideB){
hypot = sqrt((sideA ^ 2) + (sideB ^ 2))
return(hypot)
}
Now lets do the same thing with a function.
In [ ]:
triangle1 = c(3, 4, NA)
triangle2 = c(10, 15, NA)
triangle1[3] = hypotenuse(triangle1[1], triangle1[2])
triangle2[3] = hypotenuse(triangle2[1], triangle2[2])
It is not more effective, it is not significanly shorter, but it is more CLEAR TO UNDERSTAND!
Do not just write code that works, write code that is clear to read.
Let's open a file na write inside it.
In [17]:
file.create("functions-test.R")
f = file("functions-test.R", open = "w")
text =
"kidding = function(){
print('I am kidding')
}"
write(text, f)
close(f)
In [19]:
source('functions-test.R')
kidding()
In [9]:
add = function(number, number2){
number = number + number2
return(number)
}
Now what would happen if we run this:
In [10]:
number = 5
add(10, 10)
print(number)
But there is a different function.
In [11]:
num = 10
change = function(){
num = 0
}
change()
print(number)
The logic is not fully clear and is very different form other programming languages. Function searches for the closest recognizable variable near - inside function scope. If it can't find it, it uses variable in the general scope of the entire R environment.
Let's consider somehting else
In [16]:
add = function(number, number2){
number = number + number2
return(number)
}
change_add = function(){
number = 0
number2 = 5
print(add(number, number2))
}
number = 10
change_add()
In [19]:
add = function(number){
number = number + number2
return(number)
}
change_add = function(){
number = 0
number2 = 20
print(add(number))
}
number2 = 5
number = 10
change_add()
R uses so called lexical scope. Therefore it searches for variables insside function and if they are noth there, goes straight to the general environemnt. If not there, it throws an error.
more info https://darrenjw.wordpress.com/2011/11/23/lexical-scope-and-function-closures-in-r/