Write R scripts for each of the following.
a. A multiprint
function that consists of two arguments n and c that prints n copies of
character c.
b. An ismultiply function that returns TRUE if the
third argument is equal to the multiplication of the first and second
argument.
The default value of all arguments is 1.
c. An
issquare function that returns TRUE if the argument is a square number.
d. A generatetwice function that generates random numbers from 1 –
10.
The function will display the random number generated and
return the first random number that generate twice.
# Q1a
multiprint <- function(n=1,c) {
full_string <- ""
for(i in 1:n){
full_string <- paste(full_string, c)
# full_string <- rep(c, times=n)
}
print(full_string)
}
multiprint(4,"ccc")
## [1] " ccc ccc ccc ccc"
# Q1b
ismultiply <- function(num1=1,num2=1,num3=1) {
ismultiply_result <- isTRUE(num3==num1*num2)
# return(num1*num2==num3)
return(ismultiply_result)
}
ismultiply(4,3,12)
## [1] TRUE
ismultiply(5,2,15)
## [1] FALSE
# Q1c
issquare <- function(numsquare=1) {
issquare_result <- sqrt(numsquare)%%1==0
# return(sqrt(numsquare)%%1==0)
return(issquare_result)
}
issquare(7)
## [1] FALSE
issquare(9)
## [1] TRUE
# Q1d
generatetwice <- function() {
full_list <- c()
repeat {
new_num <- sample(1:10,1)
print(new_num)
if (new_num %in% full_list){
break
}
full_list <- c(full_list,new_num)
}
full_list <- c(full_list,new_num)
print(full_list)
return(new_num)
}
generatetwice()
## [1] 7
## [1] 3
## [1] 3
## [1] 7 3 3
## [1] 3
Write R scripts for each of the following.
a. A getfirstlast
function that returns the first and last elements of the argument
vector.
b. A getrows function that consists of two arguments, the
first argument is the data frame and the second argument is the number
of rows, n.
The function will return the first n rows of the data
frame.
c. A getrowbyname function that consists of two arguments,
the first argument is data frame and the second argument is name rows.
The function will return the row based on the name.
d. A
getcolumnbyname function that consists of two arguments, the first
argument is data frame and the second argument is column(s) name.
The function will return the column(s) stated.
# Q2a
getfirstlast <- function(vec) {
first_ele <- vec[1]
last_ele <- vec[length(vec)]
return(c(first_ele, last_ele))
}
vec1 <- c(8,7,5,3,1,0)
getfirstlast(vec1)
## [1] 8 0
vec2 <- c("a","c","e","g")
getfirstlast(vec2)
## [1] "a" "g"
# Q2b
getrows <- function(df,n) {
rows <- head(df,n)
return(rows)
}
getrows(mtcars,3)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Q2c
getrowbyname <- function(df,row_name) {
selected_row <- df[row_name,]
return(selected_row)
}
getrowbyname(mtcars,"Datsun 710")
## mpg cyl disp hp drat wt qsec vs am gear carb
## Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
getrowbyname(mtcars,c("Datsun 710","Fiat X1-9"))
## mpg cyl disp hp drat wt qsec vs am gear carb
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Fiat X1-9 27.3 4 79 66 4.08 1.935 18.90 1 1 4 1
# Q2d
getcolumnbyname <- function(df,col_name) {
selected_col <- df[,col_name]
return(selected_col)
}
getcolumnbyname(mtcars,c("mpg"))
## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
getcolumnbyname(mtcars,c("mpg","am","gear"))
## mpg am gear
## Mazda RX4 21.0 1 4
## Mazda RX4 Wag 21.0 1 4
## Datsun 710 22.8 1 4
## Hornet 4 Drive 21.4 0 3
## Hornet Sportabout 18.7 0 3
## Valiant 18.1 0 3
## Duster 360 14.3 0 3
## Merc 240D 24.4 0 4
## Merc 230 22.8 0 4
## Merc 280 19.2 0 4
## Merc 280C 17.8 0 4
## Merc 450SE 16.4 0 3
## Merc 450SL 17.3 0 3
## Merc 450SLC 15.2 0 3
## Cadillac Fleetwood 10.4 0 3
## Lincoln Continental 10.4 0 3
## Chrysler Imperial 14.7 0 3
## Fiat 128 32.4 1 4
## Honda Civic 30.4 1 4
## Toyota Corolla 33.9 1 4
## Toyota Corona 21.5 0 3
## Dodge Challenger 15.5 0 3
## AMC Javelin 15.2 0 3
## Camaro Z28 13.3 0 3
## Pontiac Firebird 19.2 0 3
## Fiat X1-9 27.3 1 4
## Porsche 914-2 26.0 1 5
## Lotus Europa 30.4 1 5
## Ford Pantera L 15.8 1 5
## Ferrari Dino 19.7 1 5
## Maserati Bora 15.0 1 5
## Volvo 142E 21.4 1 4
Create a data frame with the following columns name (“EID”, “EXP1”,
“EXP2”, “EXP3”)
The value of EID is from 1 to 8, and the value of
EXP1, EXP2 and EXP3 is any random number from 1-100.
a. Store the
data frame in
a csv file name numtest.csv.
After that, read
the csv file created and display the mean value for EXP1, EXP2 and EXP3.
b. Store the data frame in a csv file name numtest1.csv (use ; as
separator).
After that, read the csv file created and display the
min value for EXP1, EXP2 and EXP3.
c. Store the data frame in a
csv file name numtest.rds.
After that, read the rds file created
and display the max value for EXP1, EXP2 and EXP3.
df <- data.frame(
EID = c(1:8),
EXP1 = sample(1:100,8,replace=F),
EXP2 = sample(1:100,8,replace=F),
EXP3 = sample(1:100,8,replace=F)
)
print(df)
## EID EXP1 EXP2 EXP3
## 1 1 67 55 11
## 2 2 75 60 41
## 3 3 69 79 60
## 4 4 23 47 92
## 5 5 64 86 28
## 6 6 8 42 95
## 7 7 26 68 91
## 8 8 56 49 29
# Q3a
write.csv(df,"numtest.csv")
df_read <- read.csv("numtest.csv")
EXP1_mean <- mean(df_read$EXP1)
cat("Mean of EXP1 is", EXP1_mean)
## Mean of EXP1 is 48.5
EXP2_mean <- mean(df_read$EXP2)
cat("Mean of EXP2 is", EXP2_mean)
## Mean of EXP2 is 60.75
EXP3_mean <- mean(df_read$EXP3)
cat("Mean of EXP3 is", EXP3_mean)
## Mean of EXP3 is 55.875
Df_read <- subset(df_read, select=c(-EID,-X))
apply(Df_read,2,mean)
## EXP1 EXP2 EXP3
## 48.500 60.750 55.875
# Q3b
write.csv2(df,"numtest1.csv")
df_read2 <- read.csv("numtest1.csv", sep=";")
EXP1_min <- min(df_read2$EXP1)
cat("Min of EXP1 is", EXP1_min)
## Min of EXP1 is 8
EXP2_min <- min(df_read2$EXP2)
cat("Min of EXP2 is", EXP2_min)
## Min of EXP2 is 42
EXP3_min <- min(df_read2$EXP3)
cat("Min of EXP3 is", EXP3_min)
## Min of EXP3 is 11
# Q3c
saveRDS(df,"numtest.rds")
df_read3 <- readRDS("numtest.rds")
EXP1_max <- max(df_read3$EXP1)
cat("Max of EXP1 is", EXP1_max)
## Max of EXP1 is 75
EXP2_max <- max(df_read3$EXP2)
cat("Max of EXP2 is", EXP2_max)
## Max of EXP2 is 86
EXP3_max <- max(df_read3$EXP3)
cat("Max of EXP3 is", EXP3_max)
## Max of EXP3 is 95
Create an R file named matrixfile.r.
The script will ask user
to enter M and N.
Create a matrix with M rows and N columns with
random numbers 1-99 (no duplicate) and save it in matrix.rds.
After that, read the rds file and get the maximum and minimum position
of the matrix.
# print("Enter M and N (with space in between) :")
# mn <- readLines(con="stdin",n=1)
# MN <- as.numeric(unlist(strsplit(mn, split = " ")))
#
# Size <- MN[1]*MN[2]
# Mat1 <- matrix(sample(1:99, size=Size, replace = F) , nrow=MN[1], ncol=MN[2] , byrow = T)
#
# print(Mat1)
# saveRDS(Mat1,"matrix.RSD")
matrix <- readRDS("matrix.RSD")
print(matrix)
## [,1] [,2] [,3] [,4]
## [1,] 68 41 85 39
## [2,] 21 57 4 37
## [3,] 46 65 32 9
## [4,] 33 63 47 83
## [5,] 86 50 18 61
## [6,] 80 45 17 79
M <- nrow(matrix)
N <- ncol(matrix)
min_val <- 99
max_val <- 1
for (m in 1:M) {
for (n in 1:N) {
if (matrix[m,n] < min_val){
m_min <- m
n_min <- n
min_val <- matrix[m,n]
}
if (matrix[m,n] > max_val){
m_max <- m
n_max <- n
max_val <- matrix[m,n]
}
}
}
maxi<-which(matrix == max(matrix), arr.ind = TRUE)
mini<-which(matrix == min(matrix), arr.ind = TRUE)
print(maxi)
## row col
## [1,] 5 1
print(mini)
## row col
## [1,] 2 3
print(paste0("Position of max value is at [",m_max,",",n_max,"]"))
## [1] "Position of max value is at [5,1]"
print(paste0("Position of min value is at [",m_min,",",n_min,"]"))
## [1] "Position of min value is at [2,3]"