Beruflich Dokumente
Kultur Dokumente
library(dplyr)
library(tidyr)
library(ggplot2)
#1. Implement a for loop on the following by creating a simple example for each of them.
#a. Vector
#let me try radix sort in R
# this should have answers to the subsequent questions
##
# @input – a vector with numeric
#
##
radixSort<-function(input){
if(!is.vector(input)){
warning('Please enter a vector')
return(NA_real_)
}
if(!is.numeric(input)){
warning('Please enter a numeric vector')
return(NA_real_)
}
digits<-nchar(max(input))
input<-str_pad(input, digits, pad = "0")
rad<-data.frame(matrix(0, ncol = digits, nrow = length(input)))
for(i in 1:digits){
rad[,i] <- str_sub(input, i,i)
}
for(z in rev(1:ncol(rad))){
first <- which(rad[,z] == 0 )
second <- which(rad[,z] == 1 )
third <- which(rad[,z] == 2 )
fourth <- which(rad[,z] == 3 )
fifth <- which(rad[,z] == 4 )
sixth <- which(rad[,z] == 5 )
seventh <- which(rad[,z] == 6 )
eighth <- which(rad[,z] == 7 )
nineth <- which(rad[,z] == 8 )
tenth <- which(rad[,z] == 9 )
k<-c(first,second,third,fourth,fifth,sixth,seventh,eighth,nineth,tenth)
rad<-rad[k,]
}
# test
radixSortTest <- as.vector(c(1123,100,244,76))
radixSort(radixSortTest)
#b. Matrix
# transpose a matrix
##
# @m – a matrix as input
#
##
# test
z <- matrix(1:10, nrow=5, ncol=2)
tz <- transposeMatrix(z)
#d. List
# answer to 'a' uses a list for the index positions
#2. Implement a while loop on the following by creating a simple example for each of
them.
#a. Vector
##
# @input – a vector with numeric
#
##
##
# @arr – a vector with numeric
#@lo – lower bound
#@hi – upper bound
# returns – a list with consisting of the pivot and the rearranged vector
##
partition <- function(arr , lo , hi) {
while (TRUE){
browser()
while (start <hi && arr[inc(start)] <= pivot) {
if(start == end)
break
#inc(start)
}
#dec(end)
}
}# end of while
}# end of partition
##
# @arr – a vector with numeric
# @first – some index
# @second – other index
# returns – vector with the given indices swapped
##
## Increments an integer
# @x – an integer
#
##
## Decrements an integer
# @x – an integer
#
##
#b. Matrix
# transpose a matrix
transposeMatrix <- function(m) {
if (!is.matrix(m)) {
warning("Please enter a matrix. Return NA")
return(NA_real_)
}
tran <- matrix(1, nrow=ncol(m), ncol=nrow(m))
r <- 0
c <- 0
browser()
while (inc(r) <= nrow(m)) {
while (inc(c) <= ncol(m)) {
tran[c,r] <- m[r,c]
}
}
return(tran)
}
# test
z <- matrix(1:10, nrow=5, ncol=2)
tz <- transposeMatrix(z)
#3. Convert Icons into a data frame with 2 columns: name and yob using the following
ideas:
# a. For loop
#b. Lapply
#c. Sapply
"Brian Lara:1969",
"Virat Kohli:1988",
"AB De Villiers:1984")
# a. loop
for(icon in Icons){
icons.df[nrow(icons.df)+1,] <- c(nrow(icons.df)+1 , c(unlist(strsplit(icon, ":"))))
}
#Alternatively
rm(icons.df)
icons.df <- data.frame(ROWID= numeric(0) , ICONS= character() , DOB = character() ,
stringsAsFactors = FALSE)
cnt <- 0
for(icon in Icons){
token <- unlist(strsplit(icon , ":"))
icons.df <- rbind(icons.df , data.frame(ROWID = inc(cnt) , ICONS = token[1] , DOB = token[2] ))
}
# b sapply
# c lapply
#create data.frame using the list
lapply.list <- lapply(Icons, strsplit, ":")
icons.df.lapply <- data.frame(matrix(unlist(lapply.list), nrow=4, byrow=T))
colnames(icons.df.lapply) <- c("ICON" , "DOB")
icons.df.lapply
#4. Use the mpg dataset within ggplot2 package and visually analyse the following questions :
data(mpg)
#Correlation exists
# Downward trend
# Bigger the engine lesser the fuel economy
# I see some outliers towards the right