H2: GETTING STARTED WITH R
Running a code
- Hitting ctrl + enter
- Pressing “run code” button in provided boxes
R = language
many ways you can do things wrong
BUT also many ways to do things right
- Different people have different styles (~ language)
2.1. TYPING COMMANDS
R as simple calculator
- * = commands R will ‘execute’ that command
add 10 + 20
Substract 10-20 *
R-handleiding citeren Citation ()
Important
- Understanding how to read the extract
o YOU typed the command (ex. 10 + 20)
o You DIDN’T type the outcome (ex. [1] 30)
R printed that out as response to your command
- Understanding how the output is formatted
o Ex. 10 + 20 = 30
R printed this out as part of the respons
BUTT also printed [1]: ‘answer to the first question you asked = 30’
o Cf. later!
2.1.1. BE VERY CAREFUL TO AVOID TYPOS
R = software pretty stupid: can’t handle typos
- Assumes that you meant EXACTLY what you typed
o Ex. Didn’t press shift when trying to type 10 + 20 10 = 20
error message because command doesn’t make any sense
! some typos produce error messages
BUT others don’t because they correspond to ‘well-formed’ R commands
- Ex. Typo 10 – 20 instead of 10 + 20
R doesn’t know you meant ‘add’ instead of ‘subtract’ because you typed subtract, which is
legitimate R langue
R does EXACTLY what you ask it to do
- No ‘autocorrect’ your responsibility to be careful to avoid typos
o Always type exactly what you mean
1
,2.1.2. R IS (A BIT) FLEXIBLE WITH SPACING
! some exceptions in being exactly precise
some situations in which R is a bit flexible
- Can ignore redundant spacing (ex. 10 + 20 = 10 + 20)
o ! can’t add spaces in the middle of a word
Ex.: citation () info about how to cite R
>< citation ( ) = same respons
>< citat ion() error
2.2 DOING SIMPLE CALCULATIONS
Terminology
- Operation = action performed by the operator
- Operator = symbol that tells R which kind of calculation/operation it should do
- Ex. addition = ‘operation’
+ = operator that performs the operation
2.2.1. ADDING, SUBTRACTING, MULTIPLYING AND DIVIDING
Table: list of operators that correspond to basic arithmetic
- R: standard symbols to indicate each operation
2.2.2. TAKING POWERS
‘raising x to the n-th power’
= act of multiplying number x by itself n times
= xn
Ex. 4th power of 5: 54 = 5 x 5 x 5 x 5
Taking powers in R
- Type the complete multiplication (ex. 5*5*5*5)
- Power operator: ^ (ex. 5 ^ 4)
- Other power operator: ** (5 **4)
2
,2.2.3. DOING CALCULATIONS IN THE RIGHT ORDER
Multiple calculations? longer commands
- !! order of operations in R = BEDMAS
o Inside Brackets ()
o Exponents ^
o Devision / and Multiplication *
o Addition + and Subtraction –
Ex. 1 + 2 * 4 = 9
Ex. (1 + 2) * 4 = 12
What in case of 2 operations with the same priority?
- R goes from left right
- Ex. * 3 = 6
!! brackets ALWAYS come first
2.3. STORING A NUMBER AS A VARIABLE
Conceptual level: variable = label for certain piece(s) of information
Creating variables to store our numbers
- Suppose: calculate how much money making from selling a book
different numbers you would want to store
o How many copies selling? 1/student
350 sales: creating variable ‘sales’
o Want to assign a value to the variable
Using assignment operator: sales <-350
Assignment operator: assigning a value to a variable: <-
- ! R doesn’t print out any output
BUUTT behind the scenes: R created a variable + has given it a value
- Different ways of making assignments
o <- (most commonly used) ex. Sales <- 350 ! same operator, just in R and L form
o -> (almost never used) ex. Sales -> 350 ! don’t space between (< -: error)
o =
Also has a direction: variable = value
Other way doesn’t work
Printing the stored variable on screen
- Typing name of variable hit crtl + enter
2.4. WORKING WITH VARIABLES
Assigning new value to same variable (ex. Sales <- 250)
R overwrites old value
2.5. DOING CALCULATIONS USING VARIABLES
3
, Working further with books example
- Defining 2nd variable ‘royalty’: indicates how much money/copy (ex. $7/book)
Variable: can do anything with the information it stores (ex. 350*7)
- ALSO: multiplication with variable itself (ex. Sales * royalty)
R: numeric multiplication = multiplication with variables: same command
Can assign output of multiplication to new variable
= creating 3th variable ‘revenues’
- Command: revenue <- sales * royalty
! can also reassign the value of 3th variable, based on its current value
- Ex. One of students donates extra $550
o Command: revenue <-revenue + 550
R: took old value (2450) and added 550 value of 3000
2.6. STORING MANY NUMBERS AS A VECTOR
Vector = variable that can store multiple values
Ex. 100 booksales in February, 200 in March, 50 in April, none in the rest of the year
variable ‘sales.by.month’: stores all salesdata
- First number = 0 (0 sales in January)
- Second number = 100 (100 sales in February)
Creating vector in R: using combine function c() to store data of larger variable (ex.
Sales.by.month)
- Type all the numbers you want to store in a comma-seperated list
o Elements = all the numbers included (= between ())
o Ex. Sales.by.month <- c(0, 100, 200, 50, 0, 0, 0, 0, 0, 0, 0, 0)
Single variable = vector with 12 elements
- Flexible with use of c() function
o Works with numbers and vectors
Ex. Create new variable in more months: adding sales in a new month
Sales.by.month.extended <- c(sales.by.month, 99)
Asked R to combine vector we have + add additional number (99)
Use of c( )
- Grouping data in a vector
- Grouping datasets in a vector
- Defining variable using that variable vb. Sales.by.month <- c(sales.by.month.extended,
299)
- Making + processing datasets
- Calculating with multiple numbers at the same time
!! avoid typos
- C() only works with ()
- Vector doesn’t work
o Without anything: just c
o With []
4