Skip to content

Instantly share code, notes, and snippets.

@thatseeyou
Last active March 7, 2016 06:02
Show Gist options
  • Save thatseeyou/feb28187df1ff3832062 to your computer and use it in GitHub Desktop.
Save thatseeyou/feb28187df1ff3832062 to your computer and use it in GitHub Desktop.
R matrix exercise
#
# ex3-1.R
#
# matrix exercise
#
Q <- matrix(c(50,60,80,100,150,200,30,40,70), nrow=3)
colnames(Q) <- c("5Kg", "10Kg", "15Kg")
rownames(Q) <- c("Shop A", "Shop B", "Shop C")
P <- matrix(c(10.60, 17.20, 22.50))
colnames(P) <- c("Price")
rownames(P) <- c("5Kg", "10Kg", "15Kg")
CostPerShop <- Q %*% P
NumBags <- Q * c(18, 10, 7)
NumBagsGroupByWeight <- apply(NumBags, 2, sum)
TotalBags <- sum(NumBagsGroupByWeight)
Q
P
CostPerShop
NumBags
NumBagsGroupByWeight
TotalBags
# > Q
# 5Kg 10Kg 15Kg
# Shop A 50 100 30
# Shop B 60 150 40
# Shop C 80 200 70
#
# > P
# Price
# 5Kg 10.6
# 10Kg 17.2
# 15Kg 22.5
#
# > CostPerShop
# Price
# Shop A 2925
# Shop B 4116
# Shop C 5863
#
# > NumBags
# 5Kg 10Kg 15Kg
# Shop A 900 1800 540
# Shop B 600 1500 400
# Shop C 560 1400 490
#
# > NumBagsGroupByWeight
# 5Kg 10Kg 15Kg
# 2060 4700 1430
#
# > TotalBags
# [1] 8190
#
# ex3-2.R
#
# matrix exercise
#
y=matrix(c(1,4,7,2,5,8,3,6,9), nrow=3)
y
y * y
y + y
y + 3
1 / y
colnames(y) = c("A", "B", "C")
# select all rows except first
y[-1,]
# change all even elements to 0 using apply() function
f <- function(x) ifelse(x %% 2 == 0,0,x)
e0 <- apply(y, 2, f)
# change matrix to 9x1 matrix
dim(y) <- c(9,1)
#
# ex3-3.R
#
# matrix filtering
#
x = matrix(c(5,4,0,8,0,2,3,7,9), nrow=3)
x[c(TRUE,FALSE,TRUE), c(TRUE,TRUE,FALSE)]
x[c(TRUE,FALSE), c(2,3)]
# > x
# [,1] [,2] [,3]
# [1,] 5 8 3
# [2,] 4 0 7
# [3,] 0 2 9
# > x[c(TRUE,FALSE,TRUE), c(TRUE,TRUE,FALSE)]
# [,1] [,2]
# [1,] 5 8
# [2,] 0 2
# > x[c(TRUE,FALSE), c(2,3)]
# [,1] [,2]
# [1,] 8 3
# [2,] 2 9
#
# ex 4-1
#
a <- list(1, "abc", FALSE)
mode(a[[2]])
typeof(a[[2]])
#
# ex 4-2
#
# 4-2.1
x <- sample(10:20, 100, replace=TRUE)
find_frequency <- function(key_range, values) {
freq_list <- list()
for (i in key_range) {
freq_list[[as.character(i)]] = 0
}
for (value in values) {
freq_list[[as.character(value)]] = freq_list[[as.character(value)]] + 1
}
return(freq_list)
}
freq_list <- find_frequency(10:20, x)
# 4-2.2
find_max_frequency <- function(freq_list) {
highest = list(key='10', frequency = 0)
for (i in names(freq_list)) {
if (freq_list[[i]] > highest$frequency) {
highest$key = i
highest$frequency = freq_list[[i]]
}
}
return(highest)
}
highest_element = find_max_frequency(freq_list)
# 4-2.3
sort(freq_unlist, decreasing = TRUE)[1]
lines <- "
Jim,Hoffer,Male,52,1964.03.18,30
Sonya,Martin,Female,41,1975.09.21,12
Rachel,Darwin,Female,34,1982.05.15,5
Edward,Cruze,Male,30,1986.11.02,3
"
# read csv from string
con <- textConnection(lines)
people_info <- read.csv(con, header = FALSE, stringsAsFactors = FALSE)
close(con)
# a.
names(people_info) <- c("Name", "Surname", "Gender", "Age", "DateOfBirth", "WorkingPeriod")
# b. Display Name, Gender, Age information of peole who have Age < 50
#people_info[people_info$Age < 50, names(people_info) %in% c("Name", "Gender", "Age")]
people_info[people_info$Age < 50, c("Name", "Gender", "Age")]
# c. Change Age of Sonya to 40 and DateOfBirth to 1976.09.21
# people_info[people_info$Name == "Sonya",]$Age = 40
# people_info[people_info$Name == "Sonya",]$DateOfBirth = "1976.09.21"
people_info[people_info$Name == "Sonya", c("Age", "DateOfBirth")] <- list(40, "1976.09.21")
# d. Decrease working period of all people to 1
people_info$WorkingPeriod = people_info$WorkingPeriod - 1
sapply(people_info, typeof)
# e. Add one more information of Choonmee (Name= Choonmee, Surname = Cha, Gender = Female, Age = 29, DateOfBirth = 1987.04.26, WorkingPeriod = 5)
people_info = rbind(people_info, list("Choonmee", "Cha", "Female", 29, "1987.04.26", 5))
# f. Calculate the average Age and average WorkingPeriod of all people
apply(people_info[, c("Age", "WorkingPeriod")], 2, mean)
# g. Remove the Surname information
#people_info[, -which(names(people_info) %in% c("Surname"))]
#people_info$Surname <- NULL
people_info[, c("Surname")] <- NULL
# h. Sort the data frame by increasing WorkingPeriod
#people_info[with(people_info, order(WorkingPeriod)), ]
people_info[order(people_info$WorkingPeriod), ]
# i. Create another data frame (people_hobby) as Chapter5_pic2
lines <- "
Name,Hobby
Jim,Voleyball
Rachel,Billards
Edward,Skiing
Dasan,Music
"
con <- textConnection(lines)
people_hobby <- read.csv(con, stringsAsFactors = FALSE)
close(con)
# j. Merge the (people_info) with (peole_hobby). Save the merged data to a excel file (Chapter5_result.csv) people_info
merge(people_info, people_hobby)
merge(people_hobby, people_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment