Professional Documents
Culture Documents
2016 03 30 Cmpe 140 Computing Econ 06 Loops
2016 03 30 Cmpe 140 Computing Econ 06 Loops
Management
Lecture 6: Loops
Previous lectures
Vectors
Matrices
Lists
Data Frames
1
Vectors
Integer mode
> person.weight <- c(65, 66, 61)
Character (string)
> person.name <- c("Can", "Cem", "Hande")
Logical (Boolean)
> person.female <- c(FALSE, FALSE, TRUE)
Complex
> complex.numbers <- c(1+2i, -1+0i)
30th March 2016 Assistant Professor Dr. Bert ARNRICH 3
Vectors
Indexing
> person.height[c(T,F,T)]
Can Hande
1.70 1.62
> person.height[c(1,3)]
Can Hande
1.70 1.62
> person.height[-1]
Cem Hande
1.75 1.62
2
Vectors
Filtering
> person.height[person.height > 1.65]
Can Cem
1.72 1.75
Recycling
> c(1, 2, 3) + c(1, 2, 3, 4)
[1] 2 4 6 5
Vector operations
> person.weight / person.height^2
Can Cem Hande
21.97134 21.55102 23.24341
Matrices
Creation
> y <- matrix(c(1,2,3,4),nrow=2,ncol=2)
Matrix operations
Transposition t(y)
Element by element product y * y
Matrix multiplication y %*% y
Matrix scalar multiplication 3 * y
Matrix addition y + y
3
Matrices
Lists
Creation
> joe <- list(name="Joe", salary=55000,
staff=T)
Indexing
> joe$salary
> joe[["salary"]]
> joe[[2]]
4
Data frames
Creation
> person <- data.frame(height=person.height,
weight=person.weight)
Indexing
> person[[1]]
> person[["height"]]
> person$height
> person[c(1,2),]
> person[-3,]
Filtering
> person[person$height >= 1.7,]
Data frames
Data import
> person.data <- read.table(header=TRUE,
"height_weight_data.txt", sep=",")
Data modifications
> person.data$BMI <- person.data$Weight /
person.data$Height^2
Summary
> summary(person.data)
Merging
> merge(person.data, person.data2)
5
Program today
6
Recap: word list
7
Recap: word list
In the last step, when adding the current word to our word list
we used the two list characteristics
> word.list[[word]] <- c(word.list[[word]], i)
> my.list
$vec
[1] 1 2
30th March 2016 Assistant Professor Dr. Bert ARNRICH 16
8
Example: word list
The value for list component “a” is 1 since it is the first time that
“a” was added to the list
Let’s check again our word list after the second iteration
> word.list
$a
[1] 1
$text
[1] 2
We observe that we now have a second list entry text with position
value 2
9
Example: word list
$text
[1] 2
$consists
[1] 3
$of
[1] 4
10
Example: word list
Looping
The for loop iterates through all elements of the vector vec
11
Print variable when iterating
> vec
[1] 1 2 3 4 5
The for loop works with other modes beside numeric as well
12
Print variable when iterating
> vector.indices
[1] 1 2 3 4
13
Print variable when iterating
For example, let’s write our own function for computing the
length of a vector
> counter
[1] 10
14
Recap: writing our own function
> AddOne(1)
[1] 2
> AddOne(-5)
[1] -4
> AddOne(c(1,2,3))
[1] 2 3 4
15
Recap: writing our own function
> AddValue(1)
[1] 2
> AddValue(1,2)
[1] 3
> AddValue(c(1:3),2)
[1] 3 4 5
16
Compute length of a vector
# return counter
return(counter)
}
17
Compute norm of a vector
# sqrt of sum
norm <- sqrt(norm)
return(norm)
}
18
Compute Euclidean norm of a vector
> sqrt(1^2+2^2+3^2)
[1] 3.741657
p-norm of a vector
## compute p-norm of a vector vec
p.norm <- function(vec, p=2)
{
# initialize norm
norm <- 0
# p radical of sum
norm <- (norm)^(1/p)
return(norm)
}
19
p-norm of a vector
return(vec.res)
}
20
Square elements of a vector
> square.vec(c(1,2,3))
[1] 1 4 9
> square.vec(7:10)
[1] 49 64 81 100
> (7:10)^2
[1] 49 64 81 100
Function findwords
21
Function findwords
$text
[1] 2
$consists
[1] 3
$of
[1] 4
We can use the scan() function which read data from a file
into a vector
22
Read data from file
23
Read data from file
Working directory
> getwd()
[1] "C:/Users/Bert/Documents"
24
Change working directory in R
In the files tab we select the “…” item and browse to the
folder in which we have stored the text file
30th March 2016 Assistant Professor Dr. Bert ARNRICH 50
25
Change working directory in RStudio
> word.vec
[1] "a" "text" "consists" "of"
[5] "a" "word" "and" "another"
[9] "word" "and" "so" "on"
[13] "and" "so" "forth"
26
Function findwords
$text
[1] 2
$consists
[1] 3
$of
[1] 4
$word
[1] 6 9
...
30th March 2016 Assistant Professor Dr. Bert ARNRICH 53
Function findwords
27
Function findwords
## finds locations of each word in file
findwords <- function(file)
{
# fill word.vec from data in file
word.vec <- scan(file, "")
Function findwords
$text
[1] 2
$consists
[1] 3
$of
[1] 4
$word
[1] 6 9
...
30th March 2016 Assistant Professor Dr. Bert ARNRICH 56
28
Sort word list by word frequency
29
Sort word list by word frequency
30
Function findwords
if-else
31
if-else
> x <- 2
> y <- if(x == 2) x else x+1
> y
[1] 2
> x <- 3
> y <- if(x == 2) x else x+1
> y
[1] 4
if-else
if(sort.by.freq)
{
# sort by word frequency
return(word.list[order(sapply(word.list, length),
decreasing = T)])
}
else
{
# sort by word frequency
return(word.list[sort(names(word.list))])
}
32
Function findwords
## finds locations of each word in file
findwords <- function(file, sort.by.freq = F)
{
# fill word.vec from data in file
word.vec <- scan(file, "")
Function findwords
> findwords("text.txt")
Read 15 items
$a
[1] 1 5
$and
[1] 7 10 13
$another
[1] 8
$consists
[1] 3
$forth
[1] 15
...
30th March 2016 Assistant Professor Dr. Bert ARNRICH 66
33
Function findwords
> findwords("text.txt", sort.by.freq=T)
Read 15 items
$and
[1] 7 10 13
$a
[1] 1 5
$word
[1] 6 9
$so
[1] 11 14
$text
[1] 2
...
30th March 2016 Assistant Professor Dr. Bert ARNRICH 67
> word.freq
and a word so text
3 2 2 2 1
consists of another on forth
1 1 1 1 1
30th March 2016 Assistant Professor Dr. Bert ARNRICH 68
34
Plotting word frequencies
3.0
2.5
2.0
1.5
1.0
0.5
0.0
and
word
so
another
on
forth
text
consists
of
35
Word frequency in Wikipedia
36
Word frequency in Wikipedia
Homework
37