0% found this document useful (0 votes)
9 views20 pages

Ex 3

The document provides an overview of R version 4.4.2, including its features, syntax for variable assignment, string manipulation, and data formatting. It demonstrates various operations with vectors, lists, and matrices, showcasing how to create, access, and manipulate these data structures. Additionally, it covers error handling in R and provides examples of using functions like print, format, and sort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Ex 3

The document provides an overview of R version 4.4.2, including its features, syntax for variable assignment, string manipulation, and data formatting. It demonstrates various operations with vectors, lists, and matrices, showcasing how to create, access, and manipulate these data structures. Additionally, it covers error handling in R and provides examples of using functions like print, format, and sort.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

R version 4.4.

2 (2024-10-31 ucrt) -- "Pile of Leaves"

Copyright (C) 2024 The R Founda on for Sta s cal Compu ng

Pla orm: x86_64-w64-mingw32/x64

R is free so ware and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain condi ons.

Type 'license()' or 'licence()' for distribu on details.

Natural language support but running in an English locale

R is a collabora ve project with many contributors.

Type 'contributors()' for more informa on and

'cita on()' on how to cite R or R packages in publica ons.

Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.

> a <- 'Start and end with single quote'

> print(a)

[1] "Start and end with single quote"

> b <- "Start and end with double quotes"

> print(b)

[1] "Start and end with double quotes"

> c <- "single quote ' in between double quotes"

> print(c)

[1] "single quote ' in between double quotes"

> d <- 'Double quotes " in between single quote'

> print(d)

[1] "Double quotes \" in between single quote"


>

> e <- 'Mixed quotes"

+ print(e)

+ f <- 'Single quote ' inside single quote'

Error: unexpected symbol in:

"print(e)

f <- 'Single"

> print(f)

Error: object 'f' not found

> g <- "Double quotes " inside double quotes"

Error: unexpected symbol in "g <- "Double quotes " inside"

> print(g)

Error: object 'g' not found

>

>

> a <- "Hello"

> b <- 'How'

> c <- "are you? "

> print(paste(a,b,c))

[1] "Hello How are you? "

> print(paste(a,b,c, sep = "-"))

[1] "Hello-How-are you? "

> print(paste(a,b,c, sep = "", collapse = ""))

[1] "HelloHoware you? "

>

> # Total number of digits displayed. Last digit rounded off.

> result <- format(23.123456789, digits = 9)

> print(result)

[1] "23.1234568"

> # Display numbers in scien fic nota on.

> result <- format(c(6, 13.14521), scien fic = TRUE)


> print(result)

[1] "6.000000e+00" "1.314521e+01"

> # The minimum number of digits to the right of the decimal point.

> result <- format(23.47, nsmall = 5)

> print(result)

[1] "23.47000"

> # Format treats everything as a string.

> result <- format(6)

> print(result)

[1] "6"

> # Numbers are padded with blank in the beginning for width.

> result <- format(13.7, width = 6)

> print(result)

[1] " 13.7"

> # Le jus fy strings.

> result <- format("Hello", width = 8, jus fy = "l")

> print(result)

[1] "Hello "

> # Jus y string with center.

> result <- format("Hello", width = 8, jus fy = "c")

> print(result)

[1] " Hello "

>

>

>

> result <- nchar("Count the number of characters")

> print(result)

[1] 30

>

> # Changing to Upper case.

> result <- toupper("Changing To Upper")


> print(result)

[1] "CHANGING TO UPPER"

> # Changing to lower case.

> result <- tolower("Changing To Lower")

> print(result)

[1] "changing to lower"

>

>

>

> # Extract characters from 5th to 7th posi on.

> result <- substring("Extract", 5, 7)

> print(result)

[1] "act"

>

>

> # Atomic vector of type character.

> print("abc");

[1] "abc"

> # Atomic vector of type double.

> print(12.5)

[1] 12.5

> # Atomic vector of type integer.

> print(63L)

[1] 63

> # Atomic vector of type logical.

> print(TRUE)

[1] TRUE

> # Atomic vector of type complex.

> print(2+3i)

[1] 2+3i

> # Atomic vector of type raw.


> print(charToRaw('hello'))

[1] 68 65 6c 6c 6f

>

>

> # Crea ng a sequence from 5 to 13.

> v <- 5:13

> print(v)

[1] 5 6 7 8 9 10 11 12 13

> # Crea ng a sequence from 6.6 to 12.6.

> v <- 6.6:12.6

> print(v)

[1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6

> # If the final element specified does not belong to the sequence then it is disca

> v <- 3.8:11.4

> print(v)

[1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8

>

>

> # Create vector with elements from 5 to 9 incremen ng by 0.4.

> 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

>

>

> # The logical and numeric values are converted to characters.

> s <- c('apple','red',5,TRUE)

> print(s)

[1] "apple" "red" "5" "TRUE"

>

>

> # Accessing vector elements using posi on.

> t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")


> u <- t[c(2,3,6)]

> print(u)

[1] "Mon" "Tue" "Fri"

> # Accessing vector elements using logical indexing.

> v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]

> print(v)

[1] "Sun" "Fri"

> # Accessing vector elements using nega ve indexing.

> x <- t[c(-2,-5)]

> print(x)

[1] "Sun" "Tue" "Wed" "Fri" "Sat"

> # Accessing vector elements using 0/1 indexing.

> y <- t[c(0,0,0,0,0,0,1)]

> print(y)

[1] "Sun"

>

>

>

> # Create two vectors.

> v1 <- c(3,8,4,5,0,11)

> v2 <- c(4,11,0,8,1,2)

> # Vector addi on.

> add.result <- v1+v2

> print(add.result)

[1] 7 19 4 13 1 13

> # Vector subtrac on.

> sub.result <- v1-v2

> print(sub.result)

[1] -1 -3 4 -3 -1 9

> # Vector mul plica on.

> mul .result <- v1*v2


> print(mul .result)

[1] 12 88 0 40 0 22

> # Vector division.

> divi.result <- v1/v2

> print(divi.result)

[1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000

>

>

>

> v1 <- c(3,8,4,5,0,11)

> v2 <- c(4,11)

> # V2 becomes c(4,11,4,11,4,11)

> add.result <- v1+v2

> print(add.result)

[1] 7 19 8 16 4 22

>

>

> sub.result <- v1-v2

> print(sub.result)

[1] -1 -3 0 -6 -4 0

>

>

> v <- c(3,8,4,5,0,11, -9, 304)

> # Sort the elements of the vector.

> sort.result <- sort(v)

> print(sort.result)

[1] -9 0 3 4 5 8 11 304

> # Sort the elements in the reverse order.

> revsort.result <- sort(v, decreasing = TRUE)

> print(revsort.result)

[1] 304 11 8 5 4 3 0 -9
> # Sor ng character vectors.

> v <- c("Red","Blue","yellow","violet")

> sort.result <- sort(v)

> print(sort.result)

[1] "Blue" "Red" "violet" "yellow"

> # Sor ng character vectors in reverse order.

> revsort.result <- sort(v, decreasing = TRUE)

> print(revsort.result)

[1] "yellow" "violet" "Red" "Blue"

>

>

>

> # Create a list containing strings, numbers, vectors and a logical

> # values.

> list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)

> print(list_data)

[[1]]

[1] "Red"

[[2]]

[1] "Green"

[[3]]

[1] 21 32 11

[[4]]

[1] TRUE

[[5]]

[1] 51.23
[[6]]

[1] 119.1

>

>

> # Create a list containing a vector, a matrix and a list.

> list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),

+ list("green",12.3))

> # Give names to the elements in the list.

> names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

> # Show the 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

>

>

> # Create a list containing a vector, a matrix and a list.


> list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),

+ list("green",12.3))

>

>

>

> # Give names to the elements in the list.

> names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

> # Access the first element of the list.

> print(list_data[1])

$`1st Quarter`

[1] "Jan" "Feb" "Mar"

> # Access the thrid element. As it is also a list, all its elements will be printe

> print(list_data[3])

$`A Inner list`

$`A Inner list`[[1]]

[1] "green"

$`A Inner list`[[2]]

[1] 12.3

> # Access the list element using the name of the element.

> print(list_data$A_Matrix)

[,1] [,2] [,3]

[1,] 3 5 -2

[2,] 9 1 8

>

>

>

> # Create a list containing a vector, a matrix and a list.


> list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),

+ list("green",12.3))

> # Give names to the elements in the list.

> names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

> # Add element at the end of the list.

> list_data[4] <- "New element"

> # Remove the last element.

> list_data[4] <- NULL

> # Print the 4th Element.

> print(list_data[4])

$<NA>

NULL

> # Update the 3rd Element.

> list_data[3] <- "updated element"

> print(list_data[3])

$`A Inner list`

[1] "updated element"

>

>

> # Create two lists.

> list1 <- list(1,2,3)

> list2 <- list("Sun","Mon","Tue")

> # Merge the two lists.

> merged.list <- c(list1,list2)

> # Print the merged list.

> print(merged.list)

[[1]]

[1] 1
[[2]]

[1] 2

[[3]]

[1] 3

[[4]]

[1] "Sun"

[[5]]

[1] "Mon"

[[6]]

[1] "Tue"

>

>

> # Create lists.

> 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

> # Convert the lists to vectors.

> v1 <- unlist(list1)

> v2 <- unlist(list2)


> print(v1)

[1] 1 2 3 4 5

> print(v2)

[1] 10 11 12 13 14

> # Now add the vectors

> result <- v1+v2

> print(result)

[1] 11 13 15 17 19

>

>

>

> # Elements are arranged sequen ally by row.

> 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

> # Elements are arranged sequen ally by column.

> 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

> # Define the column and row names.

> 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

>

>

> # Define the column and row names.

> rownames = c("row1", "row2", "row3", "row4")

> colnames = c("col1", "col2", "col3")

> # Create the matrix.

> P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))

> # Access the element at 3rd column and 1st row.

> print(P[1,3])

[1] 5

> # Access the element at 2nd column and 4th row.

> print(P[4,2])

[1] 13

> # Access only the 2nd row.

> print(P[2,])

col1 col2 col3

6 7 8

> # Access only the 3rd column.

> print(P[,3])

row1 row2 row3 row4

5 8 11 14

>

>

> # Create two 2x3 matrices.

> matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)


> print(matrix1)

[,1] [,2] [,3]

[1,] 3 -1 2

[2,] 9 4 6

> matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)

> print(matrix2)

[,1] [,2] [,3]

[1,] 5 0 3

[2,] 2 9 4

> # Add the matrices.

> result <- matrix1 + matrix2

> cat("Result of addi on","\n")

Result of addi on

> print(result)

[,1] [,2] [,3]

[1,] 8 -1 5

[2,] 11 13 10

> # Subtract the matrices

> result <- matrix1 - matrix2

> cat("Result of subtrac on","\n")

Result of subtrac on

> print(result)

[,1] [,2] [,3]

[1,] -2 -1 -1

[2,] 7 -5 2

>

>

>

>

> # Create two 2x3 matrices.

> matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)


> print(matrix1)

[,1] [,2] [,3]

[1,] 3 -1 2

[2,] 9 4 6

> matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)

> print(matrix2)

[,1] [,2] [,3]

[1,] 5 0 3

[2,] 2 9 4

> # Mul ply the matrices.

> result <- matrix1 * matrix2

> cat("Result of mul plica on","\n")

Result of mul plica on

> print(result)

[,1] [,2] [,3]

[1,] 15 0 6

[2,] 18 36 24

> # Divide the matrices

> result <- matrix1 / matrix2

> cat("Result of division","\n")

Result of division

> print(result)

[,1] [,2] [,3]

[1,] 0.6 -Inf 0.6666667

[2,] 4.5 0.4444444 1.5000000

>

>

>

> # Create two vectors of different lengths.

> vector1 <- c(5,9,3)

> vector2 <- c(10,11,12,13,14,15)


> # Take these vectors as input to the array.

> result <- array(c(vector1,vector2),dim = c(3,3,2))

> print(result)

,,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

>

>

> # Create two vectors of different lengths.

> 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")

> # Take these vectors as input to the array.

> result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,colum

+ matrix.names))

Error: unexpected symbol in:

"result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,colum

matrix.names"
> print(result)

,,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

>

>

> # Create two vectors of different lengths.

> 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")

> # Take these vectors as input to the array.

> result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,

+ column.names, matrix.names))

> # Print the third row of the second matrix of the array.

> print(result[3,,2])

COL1 COL2 COL3

3 12 15

> # Print the element in the 1st row and 3rd column of the 1st matrix.
> print(result[1,3,1])

[1] 13

> # Print the 2nd Matrix.

> print(result[,,2])

COL1 COL2 COL3

ROW1 5 10 13

ROW2 9 11 14

ROW3 3 12 15

>

>

>

> # Create two vectors of different lengths.

> vector1 <- c(5,9,3)

> vector2 <- c(10,11,12,13,14,15)

> # Take these vectors as input to the array.

> array1 <- array(c(vector1,vector2),dim = c(3,3,2))

> # Create two vectors of different lengths.

> vector3 <- c(9,1,0)

> vector4 <- c(6,0,11,3,14,1,2,6,9)

> array2 <- array(c(vector1,vector2),dim = c(3,3,2))

> # create matrices from these arrays.

> matrix1 <- array1[,,2]

> matrix2 <- array2[,,2]

> # Add the matrices.

> result <- matrix1+matrix2

> print(result)

[,1] [,2] [,3]

[1,] 10 20 26

[2,] 18 22 28

[3,] 6 24 30

>
>

>

> # Create two vectors of different lengths.

> vector1 <- c(5,9,3)

> vector2 <- c(10,11,12,13,14,15)

> # Take these vectors as input to the array.

> 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

> # Use apply to calculate the sum of the rows across all the matrices.

> result <- apply(new.array, c(1), sum)

> print(result)

[1] 56 68 60

>

>

>

You might also like