## 7.10
banner_clicks <- 0
user001actions <- 0
data <- read.csv("user_traces.txt", header = FALSE )
for( rn in 1:nrow(data) ) {
user <- data[rn, 1]
action <- data[rn, 2]
if( user == 'user001' ) {
user001actions <- user001actions + 1
}
if( action == 'click banner' ) {
banner_clicks <- banner_clicks + 1
}
}
print( paste("User 001 actions", user001actions ) )
print( paste("Banner clicks", banner_clicks) )
[1] "User 001 actions 3" [1] "Banner clicks 2"
## 7.11
useractions <- list()
data <- read.csv("user_traces.txt", header = FALSE )
for( rn in 1:nrow(data) ) {
user <- data[rn, 1]
if( ! user %in% names(useractions) ) {
useractions[[user]] <- 0
}
useractions[[user]] <- useractions[[user]] + 1
}
sumofactions <- 0
number_of_users <- 0
for( action_count in unname( unlist( useractions ) ) ) {
sumofactions <- sumofactions + action_count
number_of_users <- number_of_users + 1
}
print( sumofactions / number_of_users )
highest <- 0
highest_name <- ''
lowest <- 9999
lowest_name <- ''
for( user in names( useractions ) ) {
action_count <- useractions[[ user ]]
if( action_count > highest ){
highest <- action_count
highest_name <- user
}
if( action_count < lowest ){
lowest <- action_count
lowest_name <- user
}
}
print( paste( highest_name, "had highest number of actions") )
print( paste( lowest_name, "had lowest number of actions") )
[1] 2.5 [1] "user001 had highest number of actions" [1] "user002 had lowest number of actions"
## 7.12
experimental <- list()
experimental[['A']] <- list()
experimental[['B']] <- list()
data <- read.csv("experiment.txt", header = FALSE )
for( rn in 1:nrow(data) ) {
condition <- data[rn, 2]
for( i in 3:7 ) {
experimental[[condition]] <- c( experimental[[condition]], data[rn, i] )
}
}
## means
sum_of_values <- 0
n <- 0
for( value in experimental[['A']] ) {
sum_of_values <- sum_of_values + value
n <- n + 1
}
print( paste("Mean for A", sum_of_values / n ) )
sum_of_values <- 0
n <- 0
for( value in experimental[['B']] ) {
sum_of_values <- sum_of_values + value
n <- n + 1
}
print( paste("Mean for B", sum_of_values / n ) )
[1] "Mean for A 6" [1] "Mean for B 5"
## 7.13
second_values <- list()
data <- read.csv("experiment.txt", header = FALSE )
for( rn in 1:nrow(data) ) {
second_experimental_value <- data[rn, 4]
second_values <- c( second_values, second_experimental_value )
}
sum_of_values <- 0
n <- 0
for( value in second_values ) {
sum_of_values <- sum_of_values + value
n <- n + 1
}
print( paste("Mean for second values", sum_of_values / n ) )
[1] "Mean for second values 5.5"