02 Basic Operators1
02 Basic Operators1
I. Basic operators
Topics:
Working directory
Operators
Variables
Basic data type in R
R command
Vectors
Set the working directory
getwd()
[1] "E:/R/Courses”
setwd(dir)
setwd("C:/Documents and Settings/Data")
Arithmetic and logical operators
Arithmetic Operators
The ^ operator raises the number to its left to the power of the number to its
right: for example 3^2 is 9.
The modulo returns the remainder of the division of the number to the left by
the number on its right, for example 5 modulo 3 or 5 %% 3 is 2
Calculate:
1+1
1+1
16-56
3*5
1/3
1:3
2^10 #"2 to the power of 10“
2*10
2*10^1
2*10^5 # when numbers have lots of 0's at the end, R switches to scientific notation
2*10^10
Logical Operators
v1 <- 8
v2 <- 12
%in% This operator is used to t <- 1:10
identify if an element belongs print(v1 %in% t)
to a vector. print(v2 %in% t) it produces the
%ni% Negate(`%in%`) following result −
[1] TRUE [1] FALSE
truth table
a b a&&b a || b a&&b a || b a !a !a
true true ? ? true true true ? false
true false ? ? false true false ? true
false true ? ? false true
false false ? ? false false
Variables - Basic Definition
a variable is like a box with a name
x
Basic Definition
a variable is like a box with a name
variable “type”: only certain things are allowed in the box
x
(numeric only)
Basic Definition
variable: name that refers to a value
variable type: types of values we can assign to the variable
assignment statement: assigns a value to a variable x = 5 or > x <- 5
5
x <- 5
>x
[1] 5
x
(numeric only)
Variable assignment
A variable allows you to store a value (e.g. 4) or an object (e.g. a function
description) in R. You can then later use this variable's name to easily access
the value or the object that is stored within this variable.
You can assign a value 4 to a variable my_var with the command <- or =
my_var <- 4
#You have a fruit basket with 5 apples, assign the value 5 to the variable
my_basket
Variable/Object Names
Names can contain letters, digits, and the period (dot) . symbol
Names must not contain spaces.
Underscores (_) may work but are best avoided
Names must not start with a digit.
Names must not start with the dot symbol, if it is followed by a digit
Some names are used by the system. Some are forbidden to be used, and R will tell you:
if else repeat while function for in next break
TRUE FALSE NULL Inf NaN NA
Others are used, can be overwritten, but may cause unexpected results:
c, q, t, C, D, F, I, T
diff, df, pt
# special values
5/0 # Inf = infinity
-5/0 # -Inf = negative infinity
0/0 # NaN = not a number
# "NA" = "not available", "no data"
Types of Data that are Built-in to R
data type: set of values and some operations you can apply to values
Exercise:
Instructions
my_numeric variable is 42.
my_character variable is "universe". Note that the quotation marks indicate that "universe" is a character.
my_logical variable to FALSE.
“typeof” - internal data type for storage
argument2="option")
use the <- operator
to assign the results
of the command to
your R-object
Anatomy of an R command
multiple arguments are
arguments are separated by commas
key/value pairs with a
"=" inbetween
seq(from=10, to=20)
seq(from=10, to=20, by=2)
Exercises: # Poker winnings from Monday to Friday
For poker_vector: poker_vector <- c(140, -50, 20, -120, 240)
Week1: # Roulette winnings from Monday to Friday
On Monday you won $140 roulette_vector <- …………
Tuesday you lost $50
Wednesday you won $20 Calculate the sum, maximum and minimum
Thursday you lost $120 value for each vector (use function “sum, max,
Friday you won $240 min”)
sum(……
For roulette_vector: max(…….
Week2: min(…….
On Monday you lost $24
Tuesday you lost $50 Assign the sum(poker_vector) to the variable
Wednesday you won $100 ‚week1‘ and
Thursday you lost $350 sum(roulette_vector) to the variable ‚week2‘
Friday you won $10 Week1<……..
Topics:
Working directory
Operators
Variables
Basic data type in R
R command
Vectors
Thank you!