Professional Documents
Culture Documents
2016 02 17 Cmpe 140 Computing Econ 02 Vectors
2016 02 17 Cmpe 140 Computing Econ 02 Vectors
2016 02 17 Cmpe 140 Computing Econ 02 Vectors
Management
Lecture 2: Vectors
1
Previous lecture: Exemplary course outcomes
2
Previous lecture: Lecture/Lab attendance check
Final 50%
3
Previous lecture: R download and installation
4
Previous lecture: Interactive R session
> 2 + 2
[1] 4 Result
Index of the first number on a result line: we will see later that
the index is helpful when we receive more than a single result
> exp(-2)
[1] 0.1353353
> cos(pi)
[1] -1
5
Previous lecture: Interactive R session
> y
[1] 3
> x+y
[1] 5
> x*y
[1] 6
> x*y+3
[1] 9
> sqrt(x*y)+3
[1] 5.44949
6
Previous lecture: getting help and documentation
7
Previous lecture: R Editor
8
Program today
Name conventions
Name conventions
Object names can contain letters, digits, and the dot symbol
> person1.height <- 1.70
> person1.weight <- 65
> bmi.person1 <- person1.weight /
person1.height^2
> bmi.person1
[1] 22.49135
9
Name conventions
Since names that start with a dot are special, we should not
introduce them in our scripts to avoid confusion
Descriptive names
10
Case-sensitivity
> x + x
[1] 4
> x + X
[1] 5
Names used by R
You should not use those names for your objects since it will
create confusion when other people read your script
11
Name check
> my.c
Error: object 'my.c' not found
Whenever you get the message that object is not found, you
can understand that the name is not in use
Data vectors
12
Why we need data vectors
> person.height
[1] 1.70 1.75 1.62
> person.weight
[1] 65 66 68
13
Data vector creation
For example, we have data form the first two persons and
we create a vector from it
> person.height <- c(1.70, 1.75)
Now, we extend the vector with data from the third person
> person.height <- c(person.height, 1.62)
> person.height
[1] 1.70 1.75 1.62
Integer mode
> person.weight <- c(65, 66, 61)
Character (string)
> person.name <- c("Can", "Cem", "Hande")
14
Data vector modes
Logical (Boolean)
> person.female <- c(FALSE, FALSE, TRUE)
Complex
> complex.numbers <- c(1+2i, -1+0i)
Other
Missing values
15
Data vector element names
> person.height
Can Cem Hande
1.70 1.75 1.62
> person.weight
Can Cem Hande
65 66 61
17th February 2016 Assistant Professor Dr. Bert ARNRICH 31
> person.height[1]
Can
1.7
> person.height[2]
Cem
1.75
16
Data vector indexing
17
Data vector indexing
> person.height
Can Cem Hande
1.72 1.75 1.62
18
Data vector indexing
> person.height
Can Cem Hande
1.70 1.75 1.62
> person.height
Can Cem Lale Hande
1.70 1.75 1.76 1.62
> person.height
Can Cem Lale Hande
1.70 1.75 1.76 1.62
> person.height
Can Cem Hande
1.70 1.75 1.62
19
Data vector filtering
> person.taller
Can Cem Hande
TRUE TRUE FALSE
> person.height[person.taller]
Can Cem
1.72 1.75
20
Data vector filtering
> person.weight
Can Cem Hande
65 65 61
21
Data vector filtering
> sort(person.weight)
Hande Can Cem
61 65 66
22
Data vector sorting
> order(person.height)
[1] 3 1 2
23
Data vector sorting
We can use the resulting sorted index for sorting some other
vector
> person.weight[order(person.height)]
Hande Can Cem
61 65 66
Vector recycling
24
Vector recycling
Warning message:
In c(1, 2, 3) + c(1, 2, 3, 4) :
longer object length is not a multiple of
shorter object length
25
Conditional element selection with the ifelse() function
26
Data vector operations
> bmi
Can Cem Hande
21.97134 21.55102 23.24341
27
Data vector operations
R function round
Rounding of numbers
28
Rounding of numbers
> ceiling(bmi)
Can Cem Hande
22 22 24
Rounding of numbers
> floor(bmi)
Can Cem Hande
21 21 23
29
Rounding of numbers
> trunc(bmi)
Can Cem Hande
21 21 23
Rounding of numbers
30
More data vector operations
> mean(bmi)
[1] 23.31768
R function mean
> mean(bmi)
[1] 23.31768
> length(bmi)
[1] 3
> sd(bmi)
[1] 2.294295
31
Homework 1
Collect body height and weight from 10 of your friends
Create data vectors with named elements from the first 5 persons
32
Creating regular sequences
> 11:15
[1] 11 12 13 14 15
> 3:0
[1] 3 2 1 0
Function seq
The full set of arguments can be seen from the help page
seq(from, to, by, length.out, along.with)
33
Function arguments
Function arguments
For example, let’s specify only the first two arguments from
and to
> seq(5, 10)
[1] 5 6 7 8 9 10
34
Function arguments
Function arguments
35
Creating repeated values
36
Creating repeated values with function rep
> rep(1, 3)
[1] 1 1 1
> rep(1:4, 2)
[1] 1 2 3 4 1 2 3 4
> rep(1:2,c(10,15))
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2
37
Homework 2
Name conventions
Object names can contain letters, digits, and the dot symbol
Use descriptive names in your scripts
Names are case-sensitive
Some names are already used by the system
Data vectors
Creation with construct c
Data vector modes: integer, numeric, character, logical, complex
Missing Values denoted with NA
Name vector elements to make vector more readable
38
Lessons learnt today
Data vectors
Indexing for accessing elements and subvectors
Filtering by applying evaluation function
Sorting with sort and order
Vector Recycling
Conditional Element Selection with ifelse()
Data vector operations mean, sd, length, round
Function seq
39