3. Implementing Strings in R
3A) Write an R program to demonstrate use of various string manipulation functions. [Hint : paste(),nquote(),format(),cat(),toString(),sprint()]
# Example strings string1 <- "Hello" string2 <- "World!" number <- 42 # Using paste() function for string concatenation concatenatedString <- paste(string1, string2) cat("Using paste(): ", concatenatedString, "\n") # Using noquote() function to create a quoted expression quotedString <- noquote("This is a quoted string.") cat("Using noquote(): ", as.character(quotedString), "\n") # Using format() function for formatting strings formattedString <- format(number, scientific = TRUE) cat("Using format(): ", formattedString, "\n") # Using cat() function for printing multiple strings cat("Using cat(): ", string1, string2, "\n") # Using toString() function to convert variables to strings convertedToString <- toString(c("apple", "orange", "banana")) cat("Using toString(): ", convertedToString, "\n") # Using sprintf() function for formatted string output formattedOutput <- sprintf("The value of pi is approximately %.2f", pi) cat("Using sprintf(): ", formattedOutput, "\n")
OUTPUT :
Using paste(): Hello World! Using noquote(): This is a quoted string. Using format(): 4.2e+01 Using cat(): Hello World! Using toString(): apple, orange, banana Using sprintf(): The value of pi is approximately 3.14
3B) Write an R program to to store and access string in R objects(vector,matrix,arrays,dataframes and lists).
# Vector stringVector <- c("apple", "orange", "banana") cat("Vector:\n", stringVector, "\n\n") # Matrix stringMatrix <- matrix(c("apple", "orange", "banana", "grape", "cherry", "kiwi"), nrow = 2) cat("Matrix:\n") print(stringMatrix) cat("\n") # Array stringArray <- array(c("apple", "orange", "banana", "grape", "cherry", "kiwi"), dim = c(2, 3, 1)) cat("Array:\n") print(stringArray) cat("\n") # Data Frame stringDF <- data.frame(Fruit = c("apple", "orange", "banana"), Color = c("red", "orange", "yellow")) cat("Data Frame:\n") print(stringDF) cat("\n") # List stringList <- list(Fruits = c("apple", "orange", "banana"), Colors = c("red", "orange", "yellow")) cat("List:\n") print(stringList) --> You can access elements from each of these objects using indexing <-- # Accessing elements from the vector firstElement <- stringVector[1] cat("First element of the vector:", firstElement, "\n") # Accessing elements from the matrix secondRowFirstColumn <- stringMatrix[2, 1] cat("Second row, first column of the matrix:", secondRowFirstColumn, "\n") # Accessing elements from the data frame colorOfFirstFruit <- stringDF$Color[1] cat("Color of the first fruit in the data frame:", colorOfFirstFruit, "\n") # Accessing elements from the list secondColor <- stringList$Colors[2] cat("Second color in the list:", secondColor, "\n")
OUTPUT :
Vector: apple orange banana Matrix: [,1] [,2] [,3] [1,] "apple" "banana" "cherry" [2,] "orange" "grape" "kiwi" Array: , , 1 [,1] [,2] [,3] [1,] "apple" "banana" "cherry" [2,] "orange" "grape" "kiwi" Data Frame: Fruit Color 1 apple red 2 orange orange 3banana yellow List: $Fruits [1] "apple" "orange" "banana" $Colors [1] "red" "orange" "yellow" First element of the vector: apple Second row, first column of the matrix: orange Color of the first fruit in the data frame: red Second color in the list: orange