/Users/shanrahman/Downloads/TBANLT433Rcode (1)/code1.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code2.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code3.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code4.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code5A.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code5B.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code6.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code7.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code8.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code9A.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code9B.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code10.R /Users/shanrahman/Downloads/TBANLT433Rcode (1)/code11.R— title: “Exercise 1” author: “Shan Rahman” date: “8-2-2025” output: html_document —

Introduction

This document contains the output of several R scripts for Exercise 1. Each script has been inserted into a separate code chunk.

Code from code1.R

source("code1.R", echo = TRUE)
## 
## > var.1 = c(0, 1, 2, 3)
## 
## > var.2 <- c("learn", "R")
## 
## > var.3 <- c(TRUE, 1)
## 
## > print(var.1)
## [1] 0 1 2 3
## 
## > cat("var.1 is ", var.1, "\n")
## var.1 is  0 1 2 3 
## 
## > cat("var.2 is ", var.2, "\n")
## var.2 is  learn R 
## 
## > cat("var.3 is ", var.3, "\n")
## var.3 is  1 1 
## 
## > var_x <- "Hello"
## 
## > cat("The class of var_x is ", class(var_x), "\n")
## The class of var_x is  character 
## 
## > var_x <- 34.5
## 
## > cat(" Now the class of var_x is ", class(var_x), "\n")
##  Now the class of var_x is  numeric 
## 
## > var_x <- 27
## 
## > cat(" Next the class of var_x becomes ", class(var_x), 
## +     "\n")
##  Next the class of var_x becomes  integer

Code from code10.R

source("code10.R", echo = TRUE)
## 
## > BMI <- data.frame(gender = c("Male", "Male", "Female"), 
## +     height = c(152, 171.5, 165), weight = c(81, 93, 78), Age = c(42, 
## +         38, 26))
## 
## > print(BMI)
##   gender height weight Age
## 1   Male  152.0     81  42
## 2   Male  171.5     93  38
## 3 Female  165.0     78  26
## 
## > emp.data <- data.frame(emp_id = c(1:5), emp_name = c("Rick", 
## +     "Dan", "Michelle", "Ryan", "Gary"), salary = c(623.3, 515.2, 
## +     611, 729, 84 .... [TRUNCATED] 
## 
## > print(emp.data)
##   emp_id emp_name salary start_date
## 1      1     Rick 623.30 2012-01-01
## 2      2      Dan 515.20 2013-09-23
## 3      3 Michelle 611.00 2014-11-15
## 4      4     Ryan 729.00 2014-05-11
## 5      5     Gary 843.25 2015-03-27
## 
## > str(emp.data)
## 'data.frame':    5 obs. of  4 variables:
##  $ emp_id    : int  1 2 3 4 5
##  $ emp_name  : chr  "Rick" "Dan" "Michelle" "Ryan" ...
##  $ salary    : num  623 515 611 729 843
##  $ start_date: Date, format: "2012-01-01" "2013-09-23" ...
## 
## > print(summary(emp.data))
##      emp_id    emp_name             salary        start_date        
##  Min.   :1   Length:5           Min.   :515.2   Min.   :2012-01-01  
##  1st Qu.:2   Class :character   1st Qu.:611.0   1st Qu.:2013-09-23  
##  Median :3   Mode  :character   Median :623.3   Median :2014-05-11  
##  Mean   :3                      Mean   :664.4   Mean   :2014-01-14  
##  3rd Qu.:4                      3rd Qu.:729.0   3rd Qu.:2014-11-15  
##  Max.   :5                      Max.   :843.2   Max.   :2015-03-27  
## 
## > result <- data.frame(emp.data$emp_name, emp.data$salary)
## 
## > print(result)
##   emp.data.emp_name emp.data.salary
## 1              Rick          623.30
## 2               Dan          515.20
## 3          Michelle          611.00
## 4              Ryan          729.00
## 5              Gary          843.25
## 
## > str(result)
## 'data.frame':    5 obs. of  2 variables:
##  $ emp.data.emp_name: chr  "Rick" "Dan" "Michelle" "Ryan" ...
##  $ emp.data.salary  : num  623 515 611 729 843
## 
## > result <- emp.data[1:2, ]
## 
## > print(result)
##   emp_id emp_name salary start_date
## 1      1     Rick  623.3 2012-01-01
## 2      2      Dan  515.2 2013-09-23
## 
## > emp.data$dept <- c("IT", "Operations", "IT", "HR", 
## +     "Finance")
## 
## > v <- emp.data
## 
## > print(v)
##   emp_id emp_name salary start_date       dept
## 1      1     Rick 623.30 2012-01-01         IT
## 2      2      Dan 515.20 2013-09-23 Operations
## 3      3 Michelle 611.00 2014-11-15         IT
## 4      4     Ryan 729.00 2014-05-11         HR
## 5      5     Gary 843.25 2015-03-27    Finance
## 
## > emp.data <- data.frame(emp_id = c(1:5), emp_name = c("Rick", 
## +     "Dan", "Michelle", "Ryan", "Gary"), salary = c(623.3, 515.2, 
## +     611, 729, 84 .... [TRUNCATED] 
## 
## > emp.newdata <- data.frame(emp_id = c(6:8), emp_name = c("Rasmi", 
## +     "Pranab", "Tusar"), salary = c(578, 722.5, 632.8), start_date = as.Date(c("2 ..." ... [TRUNCATED] 
## 
## > emp.finaldata <- rbind(emp.data, emp.newdata)
## 
## > print(emp.finaldata)
##   emp_id emp_name salary start_date       dept
## 1      1     Rick 623.30 2012-01-01         IT
## 2      2      Dan 515.20 2013-09-23 Operations
## 3      3 Michelle 611.00 2014-11-15         IT
## 4      4     Ryan 729.00 2014-05-11         HR
## 5      5     Gary 843.25 2015-03-27    Finance
## 6      6    Rasmi 578.00 2013-05-21         IT
## 7      7   Pranab 722.50 2013-07-30 Operations
## 8      8    Tusar 632.80 2014-06-17   Fianance

Code from code11.R

source("code11.R", echo = TRUE)
## 
## > data <- c("East", "West", "East", "North", "North", 
## +     "East", "West", "West", "West", "East", "North")
## 
## > print(data)
##  [1] "East"  "West"  "East"  "North" "North" "East"  "West"  "West"  "West" 
## [10] "East"  "North"
## 
## > print(is.factor(data))
## [1] FALSE
## 
## > factor_data <- factor(data)
## 
## > print(factor_data)
##  [1] East  West  East  North North East  West  West  West  East  North
## Levels: East North West
## 
## > print(is.factor(factor_data))
## [1] TRUE
## 
## > data <- c("East", "West", "East", "North", "North", 
## +     "East", "West", "West", "West", "East", "North")
## 
## > factor_data <- factor(data)
## 
## > print(factor_data)
##  [1] East  West  East  North North East  West  West  West  East  North
## Levels: East North West
## 
## > new_order_data <- factor(factor_data, levels = c("East", 
## +     "West", "North"))
## 
## > print(new_order_data)
##  [1] East  West  East  North North East  West  West  West  East  North
## Levels: East West North
## 
## > height <- c(132, 151, 162, 139, 166, 147, 122)
## 
## > weight <- c(48, 49, 66, 53, 67, 52, 40)
## 
## > gender <- c("male", "male", "female", "female", "male", 
## +     "female", "male")
## 
## > input_data <- data.frame(height, weight, factor(gender))
## 
## > print(input_data)
##   height weight factor.gender.
## 1    132     48           male
## 2    151     49           male
## 3    162     66         female
## 4    139     53         female
## 5    166     67           male
## 6    147     52         female
## 7    122     40           male
## 
## > str(input_data)
## 'data.frame':    7 obs. of  3 variables:
##  $ height        : num  132 151 162 139 166 147 122
##  $ weight        : num  48 49 66 53 67 52 40
##  $ factor.gender.: Factor w/ 2 levels "female","male": 2 2 1 1 2 1 2
## 
## > print(is.factor(input_data$gender))
## [1] FALSE
## 
## > print(input_data$gender)
## NULL

Code from code2.R

source("code2.R", echo = TRUE)
## 
## > v <- c(2, 5.5, 6)
## 
## > t <- c(8, 3, 4)
## 
## > print(v + t)
## [1] 10.0  8.5 10.0
## 
## > v <- c(2, 5.5, 6)
## 
## > t <- c(8, 3, 4)
## 
## > print(v - t)
## [1] -6.0  2.5  2.0
## 
## > v <- c(2, 5.5, 6)
## 
## > t <- c(8, 3, 4)
## 
## > print(v * t)
## [1] 16.0 16.5 24.0
## 
## > v <- c(2, 5.5, 6)
## 
## > t <- c(8, 3, 4)
## 
## > print(v/t)
## [1] 0.250000 1.833333 1.500000
## 
## > v <- c(2, 5.5, 6)
## 
## > t <- c(8, 3, 4)
## 
## > print(v%%t)
## [1] 2.0 2.5 2.0
## 
## > v <- c(2, 5.5, 6)
## 
## > t <- c(8, 3, 4)
## 
## > print(v%/%t)
## [1] 0 1 1
## 
## > v <- c(2, 5.5, 6)
## 
## > t <- c(8, 3, 4)
## 
## > print(v^t)
## [1]  256.000  166.375 1296.000

Code from code3.R

source("code3.R", echo = TRUE)
## 
## > x <- 5
## 
## > print(x)
## [1] 5
## 
## > x
## [1] 5
## 
## > x <- x + 1
## 
## > x
## [1] 6
## 
## > y <- 6
## 
## > x <- x + y
## 
## > x
## [1] 12
## 
## > x <- "some text"
## 
## > x
## [1] "some text"
## 
## > x <- 3.6
## 
## > x
## [1] 3.6

Code from code4.R

source("code4.R", echo = TRUE)
## 
## > x <- 5
## 
## > y <- 16
## 
## > x + y
## [1] 21
## 
## > x - y
## [1] -11
## 
## > x * y
## [1] 80
## 
## > y/x
## [1] 3.2
## 
## > y%/%x
## [1] 3
## 
## > y%%x
## [1] 1
## 
## > y^x
## [1] 1048576
## 
## > X <- 9
## 
## > x <- 9
## 
## > y <- (x - 2)%%2
## 
## > y
## [1] 1

Code from code5A.R

source("code5A.R", echo = TRUE)
## 
## > v <- c(2, 5.5, 6)
## 
## > print(v + 2)
## [1] 4.0 7.5 8.0
## 
## > v <- c(2, 5.5, 6)
## 
## > print(v - 2)
## [1] 0.0 3.5 4.0
## 
## > v <- c(2, 5.5, 6)
## 
## > print(v * 2)
## [1]  4 11 12
## 
## > v <- c(2, 5.5, 6)
## 
## > print(v/2)
## [1] 1.00 2.75 3.00

Code from code5B.R

source("code5B.R", echo = TRUE)
## 
## > x <- 25
## 
## > sqrt(x)
## [1] 5
## 
## > b <- 12
## 
## > a1 <- 3.5
## 
## > a2 <- 7.8
## 
## > x1 <- 1
## 
## > x2 <- 5
## 
## > y <- b + a1 * x1 + a2 * x2
## 
## > y
## [1] 54.5

Code from code6.R

source("code6.R", echo = TRUE)
## 
## > v1 <- c(3, 8, 4, 5, 0, 11)
## 
## > v2 <- c(4, 11)
## 
## > v3 <- v1 + v2
## 
## > v3
## [1]  7 19  8 16  4 22

Code from code7.R

source("code7.R", echo = TRUE)
## 
## > s <- c(1:5)
## 
## > s
## [1] 1 2 3 4 5
## 
## > print(seq(5, 9, by = 0.4))
##  [1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0
## 
## > s <- c("apple", "red", 5, TRUE)
## 
## > s
## [1] "apple" "red"   "5"     "TRUE" 
## 
## > class(s)
## [1] "character"
## 
## > t <- c(5, 3, 5, 6)
## 
## > t
## [1] 5 3 5 6
## 
## > class(t)
## [1] "numeric"
## 
## > t <- c("Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", 
## +     "Sat")
## 
## > u <- t[c(2, 3, 6)]
## 
## > print(u)
## [1] "Mon" "Tue" "Fri"
## 
## > v <- t[c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE)]
## 
## > print(v)
## [1] "Sun" "Fri"
## 
## > x <- t[c(-2, -5)]
## 
## > print(x)
## [1] "Sun" "Tue" "Wed" "Fri" "Sat"
## 
## > y <- t[c(0, 0, 0, 0, 0, 0, 1)]
## 
## > print(y)
## [1] "Sun"

Code from code8.R

source("code8.R", echo = TRUE)
## 
## > list_data <- list(c("Jan", "Feb", "Mar"), matrix(c(3, 
## +     9, 5, 1, -2, 8), nrow = 2), list("green", 12.3))
## 
## > names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
## 
## > print(list_data)
## $`1st Quarter`
## [1] "Jan" "Feb" "Mar"
## 
## $A_Matrix
##      [,1] [,2] [,3]
## [1,]    3    5   -2
## [2,]    9    1    8
## 
## $`A Inner list`
## $`A Inner list`[[1]]
## [1] "green"
## 
## $`A Inner list`[[2]]
## [1] 12.3
## 
## 
## 
## > list_data <- list(c("Jan", "Feb", "Mar"), matrix(c(3, 
## +     9, 5, 1, -2, 8), nrow = 2), list("green", 12.3))
## 
## > names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
## 
## > list_data[4] <- "New element"
## 
## > print(list_data[4])
## [[1]]
## [1] "New element"
## 
## 
## > list_data[4] <- NULL
## 
## > print(list_data[4])
## $<NA>
## NULL
## 
## 
## > list_data[3] <- "updated element"
## 
## > print(list_data[3])
## $`A Inner list`
## [1] "updated element"
## 
## 
## > list1 <- list(1:5)
## 
## > print(list1)
## [[1]]
## [1] 1 2 3 4 5
## 
## 
## > list2 <- list(10:14)
## 
## > print(list2)
## [[1]]
## [1] 10 11 12 13 14
## 
## 
## > v1 <- unlist(list1)
## 
## > v2 <- unlist(list2)
## 
## > print(v1)
## [1] 1 2 3 4 5
## 
## > print(v2)
## [1] 10 11 12 13 14
## 
## > result <- v1 + v2
## 
## > print(result)
## [1] 11 13 15 17 19
## 
## > list1 <- list(c(2, 5, 3), 21.3, sin)
## 
## > print(list1)
## [[1]]
## [1] 2 5 3
## 
## [[2]]
## [1] 21.3
## 
## [[3]]
## function (x)  .Primitive("sin")
## 
## 
## > list1[[1]]
## [1] 2 5 3
## 
## > list2 <- list(c(2, 5, 3), 21.3, sin(30))
## 
## > print(list2)
## [[1]]
## [1] 2 5 3
## 
## [[2]]
## [1] 21.3
## 
## [[3]]
## [1] -0.9880316
## 
## 
## > list2[[1]][[2]]
## [1] 5
## 
## > list3 <- list(list1, list2)
## 
## > list3
## [[1]]
## [[1]][[1]]
## [1] 2 5 3
## 
## [[1]][[2]]
## [1] 21.3
## 
## [[1]][[3]]
## function (x)  .Primitive("sin")
## 
## 
## [[2]]
## [[2]][[1]]
## [1] 2 5 3
## 
## [[2]][[2]]
## [1] 21.3
## 
## [[2]][[3]]
## [1] -0.9880316
## 
## 
## 
## > list3[[1]][[1]]
## [1] 2 5 3
## 
## > str(list3)
## List of 2
##  $ :List of 3
##   ..$ : num [1:3] 2 5 3
##   ..$ : num 21.3
##   ..$ :function (x)  
##  $ :List of 3
##   ..$ : num [1:3] 2 5 3
##   ..$ : num 21.3
##   ..$ : num -0.988
## 
## > class(list3)
## [1] "list"

Code from code9A.R

source("code9A.R", echo = TRUE)
## 
## > M = matrix(c("a", "a", "b", "c", "b", "a"), nrow = 2, 
## +     ncol = 3, byrow = TRUE)
## 
## > print(M)
##      [,1] [,2] [,3]
## [1,] "a"  "a"  "b" 
## [2,] "c"  "b"  "a" 
## 
## > a <- array(c("green", "yellow"), dim = c(3, 3, 2))
## 
## > print(a)
## , , 1
## 
##      [,1]     [,2]     [,3]    
## [1,] "green"  "yellow" "green" 
## [2,] "yellow" "green"  "yellow"
## [3,] "green"  "yellow" "green" 
## 
## , , 2
## 
##      [,1]     [,2]     [,3]    
## [1,] "yellow" "green"  "yellow"
## [2,] "green"  "yellow" "green" 
## [3,] "yellow" "green"  "yellow"
## 
## 
## > b <- array(c(1:20), dim = c(4, 5))
## 
## > print(b)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    5    9   13   17
## [2,]    2    6   10   14   18
## [3,]    3    7   11   15   19
## [4,]    4    8   12   16   20
## 
## > c <- array(c(1:20), dim = c(2, 5))
## 
## > print(c)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
## 
## > c <- array(c(1:20), dim = c(7, 5))
## 
## > print(c)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    8   15    2    9
## [2,]    2    9   16    3   10
## [3,]    3   10   17    4   11
## [4,]    4   11   18    5   12
## [5,]    5   12   19    6   13
## [6,]    6   13   20    7   14
## [7,]    7   14    1    8   15
## 
## > d <- b + 10
## 
## > d
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   11   15   19   23   27
## [2,]   12   16   20   24   28
## [3,]   13   17   21   25   29
## [4,]   14   18   22   26   30
## 
## > e <- array(c(1:10), dim = c(5, 2))
## 
## > f <- b %*% e
## 
## > f
##      [,1] [,2]
## [1,]  175  400
## [2,]  190  440
## [3,]  205  480
## [4,]  220  520
## 
## > a = c(1, 2, 3)
## 
## > b = c(2, 4, 6)
## 
## > c = cbind(a, b)
## 
## > c
##      a b
## [1,] 1 2
## [2,] 2 4
## [3,] 3 6
## 
## > str(c)
##  num [1:3, 1:2] 1 2 3 2 4 6
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:2] "a" "b"
## 
## > x = c(2, 2, 2)
## 
## > a * b
## [1]  2  8 18
## 
## > b * a
## [1]  2  8 18
## 
## > x %*% c
##       a  b
## [1,] 12 24

Code from code9B.R

source("code9B.R", echo = TRUE)
## 
## > a <- array(c("green", "yellow"), dim = c(3, 3, 2))
## 
## > print(a)
## , , 1
## 
##      [,1]     [,2]     [,3]    
## [1,] "green"  "yellow" "green" 
## [2,] "yellow" "green"  "yellow"
## [3,] "green"  "yellow" "green" 
## 
## , , 2
## 
##      [,1]     [,2]     [,3]    
## [1,] "yellow" "green"  "yellow"
## [2,] "green"  "yellow" "green" 
## [3,] "yellow" "green"  "yellow"
## 
## 
## > vector1 <- c(5, 9, 3)
## 
## > vector2 <- c(10, 11, 12, 13, 14, 15)
## 
## > column.names <- c("COL1", "COL2", "COL3")
## 
## > row.names <- c("ROW1", "ROW2", "ROW3")
## 
## > matrix.names <- c("Matrix1", "Matrix2")
## 
## > result <- array(c(vector1, vector2), dim = c(3, 3, 
## +     2), dimnames = list(row.names, column.names, matrix.names))
## 
## > print(result)
## , , Matrix1
## 
##      COL1 COL2 COL3
## ROW1    5   10   13
## ROW2    9   11   14
## ROW3    3   12   15
## 
## , , Matrix2
## 
##      COL1 COL2 COL3
## ROW1    5   10   13
## ROW2    9   11   14
## ROW3    3   12   15
## 
## 
## > M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
## 
## > print(M)
##      [,1] [,2] [,3]
## [1,]    3    4    5
## [2,]    6    7    8
## [3,]    9   10   11
## [4,]   12   13   14
## 
## > N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
## 
## > print(N)
##      [,1] [,2] [,3]
## [1,]    3    7   11
## [2,]    4    8   12
## [3,]    5    9   13
## [4,]    6   10   14
## 
## > rownames = c("row1", "row2", "row3", "row4")
## 
## > colnames = c("col1", "col2", "col3")
## 
## > P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, 
## +     colnames))
## 
## > print(P)
##      col1 col2 col3
## row1    3    4    5
## row2    6    7    8
## row3    9   10   11
## row4   12   13   14
## 
## > rownames = c("row1", "row2", "row3", "row4")
## 
## > colnames = c("col1", "col2", "col3")
## 
## > P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, 
## +     colnames))
## 
## > P
##      col1 col2 col3
## row1    3    4    5
## row2    6    7    8
## row3    9   10   11
## row4   12   13   14
## 
## > print(P[1, 3])
## [1] 5
## 
## > print(P[4, 2])
## [1] 13
## 
## > print(P[2, ])
## col1 col2 col3 
##    6    7    8 
## 
## > print(P[, 3])
## row1 row2 row3 row4 
##    5    8   11   14 
## 
## > P[, "col3"]
## row1 row2 row3 row4 
##    5    8   11   14 
## 
## > vector1 <- c(5, 9, 3)
## 
## > vector2 <- c(10, 11, 12, 13, 14, 15)
## 
## > new.array <- array(c(vector1, vector2), dim = c(3, 
## +     3, 2))
## 
## > print(new.array)
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    5   10   13
## [2,]    9   11   14
## [3,]    3   12   15
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    5   10   13
## [2,]    9   11   14
## [3,]    3   12   15
## 
## 
## > result <- apply(new.array, c(1), sum)
## 
## > print(result)
## [1] 56 68 60
## 
## > result <- apply(new.array, c(2), sum)
## 
## > print(result)
## [1] 34 66 84