Statistiek 3
Hst 2: getting started with R
2.1 Typing commands
G typo’s
Spacing maakt n uit/ tenzij in een woord
add 10 + 20
Substract 10-20
R-handleiding citeren Citation ()
2.2 Doing simple calculations
Vb operation = addition
- Operator = +
Order of operations: BEDMAS
- Brackets ()
- Exponents ^
- Devision / and Multiplication *
- Addition + and Substraction –
2.3. Storing a nr as a variable
Assign a value to variable: assignment operator Sales <- 350
350 -> sales
Sales < - 350
Sales = 350
350 = sales
2.4. Working with variables
Assign new value to variable Sales <- 250
Sales
2.5. Doing calculations using variables
Multiplication Sales <- 350
1
, Royalty <- 7
----------------------------------------------------------------
Sales * royalty (= 350 * 7)
Assign output calculation to new variable Revenue <- sales * royalty
Revenue
Overwrite previous value (donation of 550 €) Revenue <- revenue + 550
Revenue
2.6. Storing many nr’s as a vector
Vector = variable that can store multiple values
- Between ( ) elements (12 in vb)
Combine function C () Sales.by.month <- c(0, 100, 200, 50, 0, 0, 0, 0, 0, 0, 0, 0)
Sales.by.month
Flexible with c () works on nr’s and vectors at the same time
Add sales in a new month Sales.by.month.extended <- c(sales.by.month, 99)
Sales.by.month.extended
C( ) wordt gebruikt voor
Data te groeperen in een vector
Dataset te maken en te verwerken
Rekenen met meerdere getallen tegelijk
2.7. Working with a vector
Get info out of a vector Sales.by.month[2]
- boeken in feb
Create new variables February.sales <- sales.by.month[2]
February.sales
Get access to more elements Sales.by.month[c(1,2,3)]
Change a value stored in a vector Sales.by.month[5] <- 25
- 25 extra boeken in mei Sales.by.month
Sales.by.month [c(1,2,3,4,5,6)] Sales.by.month [1:6]
Use it to alter (wijzig) elements of a vector Sales.by.month[3:7] <- 2
Sales.by.month
2
, 3 nr’s wijzigen en je geeft er twee lukt niet
4 wijzigen en je geeft er 2 lukt wel recycling rule
2.8. Doing calculation with vectors
Alter all the elements of a vector at once (+/*-) Sales.by.month * 7
- = profit ----------------------------------------------------------------
0 700 1400 350 0 0 0 0 0 0 0 0
Bonus 1€ in jan, 2 in feb… Bonus <- 1:12
- The nth element of bonus is added Bonus
to the nth element of profit
Profit_bonus <- profit + bonus
Profit_bonus
---------------------------------------------------------------
1 702 1403 354 5 6 7 8 9 10 11 12
Wil weten hoeveel geld je per dag krijgt Days.per.month <- c(31, 28, 31, 30, 31, 30, 31,
- Divide every element of profit by 30, 31, 30, 31, 30, 31)
the corresponding element of
days.per.month Profit/days.per.month
The recycling rule
R has recycled the value of the shorter vector Y several times
- The first element of x is added to the first element of y
- Second element of x is added to the second element of y
- Third element of x isn’t any corresponding element in y returns to the beginning
R also does it when the length of the longer vector isn’t an exact multiple of the length of the shorter one
- Gives warning, but we will ignore it
3
, 2.9. Using functions to do calculations
Square root function Sqrt ()
225th element of sqrt Sqrt []
Absolute value function Abs ()
Use a function to do something Calling the function
Values typed into the function Arguments of that functions
1. Calculate value abs (-8)
2. Sqrt (1 + 8)
Sqrt (c (25, 49, 36)) 576
Does it on each element separately = vector element-wise
Know how many elements there are in a vector length ()
2.10 Combining stuff and the work-from-within-rule
Sqrt( abs(-25) ) First take the absolute value of -25 and then take the
square root
= work-from-within-rule
4
Hst 2: getting started with R
2.1 Typing commands
G typo’s
Spacing maakt n uit/ tenzij in een woord
add 10 + 20
Substract 10-20
R-handleiding citeren Citation ()
2.2 Doing simple calculations
Vb operation = addition
- Operator = +
Order of operations: BEDMAS
- Brackets ()
- Exponents ^
- Devision / and Multiplication *
- Addition + and Substraction –
2.3. Storing a nr as a variable
Assign a value to variable: assignment operator Sales <- 350
350 -> sales
Sales < - 350
Sales = 350
350 = sales
2.4. Working with variables
Assign new value to variable Sales <- 250
Sales
2.5. Doing calculations using variables
Multiplication Sales <- 350
1
, Royalty <- 7
----------------------------------------------------------------
Sales * royalty (= 350 * 7)
Assign output calculation to new variable Revenue <- sales * royalty
Revenue
Overwrite previous value (donation of 550 €) Revenue <- revenue + 550
Revenue
2.6. Storing many nr’s as a vector
Vector = variable that can store multiple values
- Between ( ) elements (12 in vb)
Combine function C () Sales.by.month <- c(0, 100, 200, 50, 0, 0, 0, 0, 0, 0, 0, 0)
Sales.by.month
Flexible with c () works on nr’s and vectors at the same time
Add sales in a new month Sales.by.month.extended <- c(sales.by.month, 99)
Sales.by.month.extended
C( ) wordt gebruikt voor
Data te groeperen in een vector
Dataset te maken en te verwerken
Rekenen met meerdere getallen tegelijk
2.7. Working with a vector
Get info out of a vector Sales.by.month[2]
- boeken in feb
Create new variables February.sales <- sales.by.month[2]
February.sales
Get access to more elements Sales.by.month[c(1,2,3)]
Change a value stored in a vector Sales.by.month[5] <- 25
- 25 extra boeken in mei Sales.by.month
Sales.by.month [c(1,2,3,4,5,6)] Sales.by.month [1:6]
Use it to alter (wijzig) elements of a vector Sales.by.month[3:7] <- 2
Sales.by.month
2
, 3 nr’s wijzigen en je geeft er twee lukt niet
4 wijzigen en je geeft er 2 lukt wel recycling rule
2.8. Doing calculation with vectors
Alter all the elements of a vector at once (+/*-) Sales.by.month * 7
- = profit ----------------------------------------------------------------
0 700 1400 350 0 0 0 0 0 0 0 0
Bonus 1€ in jan, 2 in feb… Bonus <- 1:12
- The nth element of bonus is added Bonus
to the nth element of profit
Profit_bonus <- profit + bonus
Profit_bonus
---------------------------------------------------------------
1 702 1403 354 5 6 7 8 9 10 11 12
Wil weten hoeveel geld je per dag krijgt Days.per.month <- c(31, 28, 31, 30, 31, 30, 31,
- Divide every element of profit by 30, 31, 30, 31, 30, 31)
the corresponding element of
days.per.month Profit/days.per.month
The recycling rule
R has recycled the value of the shorter vector Y several times
- The first element of x is added to the first element of y
- Second element of x is added to the second element of y
- Third element of x isn’t any corresponding element in y returns to the beginning
R also does it when the length of the longer vector isn’t an exact multiple of the length of the shorter one
- Gives warning, but we will ignore it
3
, 2.9. Using functions to do calculations
Square root function Sqrt ()
225th element of sqrt Sqrt []
Absolute value function Abs ()
Use a function to do something Calling the function
Values typed into the function Arguments of that functions
1. Calculate value abs (-8)
2. Sqrt (1 + 8)
Sqrt (c (25, 49, 36)) 576
Does it on each element separately = vector element-wise
Know how many elements there are in a vector length ()
2.10 Combining stuff and the work-from-within-rule
Sqrt( abs(-25) ) First take the absolute value of -25 and then take the
square root
= work-from-within-rule
4