0% found this document useful (0 votes)
16 views22 pages

02 Basic Operators1

The document introduces basic operators, variables, and data types in R including arithmetic, logical, and assignment operators as well as numeric, character, and logical data types. It provides examples of using various operators to perform calculations and comparisons, defines variables and assigns values to them, and discusses the built-in data types in R and how to check the class and type of different variables.

Uploaded by

the killerboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

02 Basic Operators1

The document introduces basic operators, variables, and data types in R including arithmetic, logical, and assignment operators as well as numeric, character, and logical data types. It provides examples of using various operators to perform calculations and comparisons, defines variables and assigns values to them, and discusses the built-in data types in R and how to check the class and type of different variables.

Uploaded by

the killerboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to

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

Operator Description >1+1


+ addition [1] 2
- subtraction > 5/2
[1] 2.5
* multiplication
> 4**
/ division + 4**2
^ or ** exponentiation [1] 4294967296
x %% y modulus (x mod y) > 2^2
5%%2 is 1 [1] 4
x %/% y integer division > 2**2
5%/%2 is 2 [1] 4

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

Operator Description > 4 == 4


< less than [1] TRUE
<= less than or equal to > 4!=4
[1] FALSE
> greater than
> 7>=6
>= greater than or equal [1] TRUE
to
> "hello" == "bye bye"
== exactly equal to [1] FALSE
!= not equal to > "hello" > "Good afternnon" #Alphabetical Order!
!x Not x [1] TRUE
x|y x OR y > TRUE < FALSE #TRUE = 1, FALSE = 0
[1] FALSE
x&y x AND y
isTRUE(x) test if X is TRUE
Miscellaneous Operators
These operators are used to for specific purpose and not general
mathematical or logical computation.
Operator Description Example
v <- 2:8
Colon operator. It creates the print(v) it produces the following
: series of numbers in sequence result −
for a vector. [1] 2 3 4 5 6 7 8

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

M = matrix( c(2,6,5,1,10,4), nrow =


2,ncol = 3,byrow = TRUE)
This operator is used to t = M %*% t(M)
%*% multiply a matrix with its print(t)it produces the following
transpose. result −
[,1] [,2] [1,] 65 82 [2,] 82 117
https://www.tutorialspoint.com/r/r_operators.htm
Assignment Operators
These operators are used to assign values to vectors .

Operator Description Example


v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
<− v3 = c(3,1,TRUE,2+3i)
or print(v1)
= Called Left Assignment print(v2)
or print(v3)it produces the following result −
<<− [1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
c(3,1,TRUE,2+3i) -> v1
-> c(3,1,TRUE,2+3i) ->> v2 print(v1)
or Called Right Assignment print(v2)it produces the following result −
->> [1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
Booleans
useful to control logic and control flow of your program
type set of values examples: literal operations
values
boolean true or false true and (&&), or (||),
false not(!)

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

# Assign the value 20 to x


x ……
# Print out the value of the variable x
X

#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

type set of values examples: operations


literal values
char characters “x” compare
“Mary”
characters
String sequences of “Hello World” concatenate
characters “ABC &%$”
int integers 42 add, subtract,
12345 multiply, divide
numeric
double floating-point 2.71828 add, subtract,
numbers 3.14e2 multiply, divide
boolean truth values true and, or, not,
false xor
Basic data types in R

Decimals values like 4.5 are called numeric.


Natural numbers like 4 are called integers. Integers are also numeric.
Boolean values (TRUE or FALSE) are called logical.
Text (or string) values are called characters (“Mary”).
Note how the quotation marks indicate that "some text" is a character.

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

# Change my_numeric to be 42 # Check class and typeof of my_numeric


my_numeric <- 42.5 class(my_numeric)
# Change my_character to be "universe“ typeof(my_numeric)
my_character <- "some text“ # Check class of my_character
# Change my_logical to be FALSE …………
my_logical <- TRUE # Check class of my_logical
…………
Anatomy of an R command

define a name for a arguments (data-objects to


the actual
(new) R-object that will be used, options & settings)
command/function
store the results of the enclosed in parantheses
command

my.object <- command(argument1=data.object,

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

no quotation marks for R-


my.object <- command(argument1=data.object, objects or logical inputs
argument2="option") (TRUE, FALSE)
quotation marks around
strings of text

my.object <- command(data.object, "option")


the argument names can be omitted, if arguments are given
in the correct order
me <- c(2, 3, 4, 6)
Mary <- c(4, 5, 2)
My.data <- read.csv("file.csv", header = TRUE)us <- sum(me, Mary)
> us
[1] 26
 () functions, mathematical expressions
 [] indexing
 {} programming, grouping of commands
Create a vector
Vectors are one-dimension arrays that can hold numeric data, character
data, or logical data. In other words, a vector is a simple tool to store
data.
In R, you create a vector with the combine function c(). You place the
vector elements separated by a comma between the parentheses.
my.vector <- c(1,2,3,4,5) # "c" stands for "concatenate"
my.vector

my.new.vector <- 1:5


5:1

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!

You might also like