7b.
# Demonstration of R Objects
# ------------------------------------------
# 1. Vector (homogeneous data type)
numeric_vector <- c(1, 2, 3, 4, 5)
character_vector <- c("apple", "banana", "cherry")
print("Numeric Vector:")
print(numeric_vector)
print("Character Vector:")
print(character_vector)
# 2. Matrix (2D, same data type)
matrix_obj <- matrix(1:9, nrow = 3, ncol = 3)
print("Matrix:")
print(matrix_obj)
# 3. List (heterogeneous data types)
list_obj <- list(name = "John", age = 25, scores = c(88, 90, 95))
print("List:")
print(list_obj)
# Access elements in list
print(paste("Name from list:", list_obj$name))
# 4. Data Frame (tabular data structure)
df <- data.frame(
    ID = 1:3,
    Name = c("Alice", "Bob", "Charlie"),
    Score = c(85, 90, 88)
print("Data Frame:")
print(df)
# 5. Factor (categorical variable)
colors <- factor(c("red", "blue", "green", "red", "blue"))
print("Factor:")
print(colors)
print("Levels of Factor:")
print(levels(colors))
# 6. Function (user-defined)
add_numbers <- function(a, b) {
    return(a + b)
result <- add_numbers(10, 20)
print(paste("Result of function add_numbers(10, 20):", result))
# 7. Logical Object
logical_vector <- c(TRUE, FALSE, TRUE, TRUE)
print("Logical Vector:")
print(logical_vector)
# 8. NULL and NA
null_object <- NULL
na_value <- NA
print("NULL Object:")
print(null_object)
print("NA Value:")
print(na_value)
out-put
# Demonstration of R Objects
> # ------------------------------------------
>
> # 1. Vector (homogeneous data type)
> numeric_vector <- c(1, 2, 3, 4, 5)
> character_vector <- c("apple", "banana", "cherry")
>
> print("Numeric Vector:")
[1] "Numeric Vector:"
> print(numeric_vector)
[1] 1 2 3 4 5
>
> print("Character Vector:")
[1] "Character Vector:"
> print(character_vector)
[1] "apple" "banana" "cherry"
>
> # 2. Matrix (2D, same data type)
> matrix_obj <- matrix(1:9, nrow = 3, ncol = 3)
> print("Matrix:")
[1] "Matrix:"
> print(matrix_obj)
    [,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
>
> # 3. List (heterogeneous data types)
> list_obj <- list(name = "John", age = 25, scores = c(88, 90, 95))
> print("List:")
[1] "List:"
> print(list_obj)
$name
[1] "John"
$age
[1] 25
$scores
[1] 88 90 95
>
> # Access elements in list
> print(paste("Name from list:", list_obj$name))
[1] "Name from list: John"
>
> # 4. Data Frame (tabular data structure)
> df <- data.frame(
+ ID = 1:3,
+ Name = c("Alice", "Bob", "Charlie"),
+ Score = c(85, 90, 88)
+)
> print("Data Frame:")
[1] "Data Frame:"
> print(df)
 ID Name Score
1 1 Alice 85
2 2      Bob 90
3 3 Charlie 88
>
> # 5. Factor (categorical variable)
> colors <- factor(c("red", "blue", "green", "red", "blue"))
> print("Factor:")
[1] "Factor:"
> print(colors)
[1] red blue green red blue
Levels: blue green red
> print("Levels of Factor:")
[1] "Levels of Factor:"
> print(levels(colors))
[1] "blue" "green" "red"
>
> # 6. Function (user-defined)
> add_numbers <- function(a, b) {
+ return(a + b)
+}
> result <- add_numbers(10, 20)
> print(paste("Result of function add_numbers(10, 20):", result))
[1] "Result of function add_numbers(10, 20): 30"
>
> # 7. Logical Object
> logical_vector <- c(TRUE, FALSE, TRUE, TRUE)
> print("Logical Vector:")
[1] "Logical Vector:"
> print(logical_vector)
[1] TRUE FALSE TRUE TRUE
>
> # 8. NULL and NA
> null_object <- NULL
> na_value <- NA
> print("NULL Object:")
[1] "NULL Object:"
> print(null_object)
NULL
> print("NA Value:")
[1] "NA Value:"
> print(na_value)
9. Write an R script to read and write different files.
Here's a simple R script that demonstrates how to read from and write to different types of
files, including:
    1.   CSV files
    2.   Excel files
    3.   Text files
    4.   JSON files
    5.   RDS files
Make sure you have the required packages installed. You can install them with:
# Load necessary libraries
library(readr)    # For reading/writing CSV and text files
library(readxl) # For reading Excel files
library(writexl) # For writing Excel files
library(jsonlite) # For reading/writing JSON
# 1. CSV FILES ----------------------------------
# Reading a CSV file
csv_data <- read_csv("data/input_data.csv")
print("CSV Data:")
print(head(csv_data))
# Writing a CSV file
write_csv(csv_data, "output/output_data.csv")
# 2. EXCEL FILES --------------------------------
# Reading an Excel file (first sheet)
excel_data <- read_excel("data/input_data.xlsx")
print("Excel Data:")
print(head(excel_data))
# Writing to Excel
write_xlsx(excel_data, "output/output_data.xlsx")
# 3. TEXT FILES ---------------------------------
# Reading a plain text file (line by line)
text_lines <- read_lines("data/sample_text.txt")
print("Text Lines:")
print(head(text_lines))
# Writing to a text file
write_lines(text_lines, "output/output_text.txt")
# 4. JSON FILES ---------------------------------
# Reading JSON file
json_data <- fromJSON("data/input_data.json")
print("JSON Data:")
print(head(json_data))
# Writing JSON file
write_json(json_data, "output/output_data.json", pretty = TRUE)
# 5. RDS FILES ----------------------------------
# Save an R object to RDS
saveRDS(csv_data, "output/data_object.rds")
# Load it back
loaded_data <- readRDS("output/data_object.rds")
print("Loaded RDS Data:")
print(head(loaded_data))
OUT-PUT
# Load necessary libraries
> library(readr)     # For reading/writing CSV and text files
Error in library(readr) : there is no package called ‘readr’
> library(readxl) # For reading Excel files
Error in library(readxl) : there is no package called ‘readxl’
> library(writexl) # For writing Excel files
Error in library(writexl) : there is no package called ‘writexl’
> library(jsonlite) # For reading/writing JSON
Error in library(jsonlite) : there is no package called ‘jsonlite’
>
> # 1. CSV FILES ----------------------------------
>
> # Reading a CSV file
> csv_data <- read_csv("data/input_data.csv")
Error in read_csv("data/input_data.csv") :
 could not find function "read_csv"
> print("CSV Data:")
[1] "CSV Data:"
> print(head(csv_data))
Error: object 'csv_data' not found
>
> # Writing a CSV file
> write_csv(csv_data, "output/output_data.csv")
Error in write_csv(csv_data, "output/output_data.csv") :
 could not find function "write_csv"
>
> # 2. EXCEL FILES --------------------------------
>
> # Reading an Excel file (first sheet)
> excel_data <- read_excel("data/input_data.xlsx")
Error in read_excel("data/input_data.xlsx") :
 could not find function "read_excel"
> print("Excel Data:")
[1] "Excel Data:"
> print(head(excel_data))
Error: object 'excel_data' not found
>
> # Writing to Excel
> write_xlsx(excel_data, "output/output_data.xlsx")
Error in write_xlsx(excel_data, "output/output_data.xlsx") :
 could not find function "write_xlsx"
>
> # 3. TEXT FILES ---------------------------------
>
> # Reading a plain text file (line by line)
> text_lines <- read_lines("data/sample_text.txt")
Error in read_lines("data/sample_text.txt") :
 could not find function "read_lines"
> print("Text Lines:")
[1] "Text Lines:"
> print(head(text_lines))
Error: object 'text_lines' not found
>
> # Writing to a text file
> write_lines(text_lines, "output/output_text.txt")
Error in write_lines(text_lines, "output/output_text.txt") :
 could not find function "write_lines"
>
> # 4. JSON FILES ---------------------------------
>
> # Reading JSON file
> json_data <- fromJSON("data/input_data.json")
Error in fromJSON("data/input_data.json") :
 could not find function "fromJSON"
> print("JSON Data:")
[1] "JSON Data:"
> print(head(json_data))
Error: object 'json_data' not found
>
> # Writing JSON file
> write_json(json_data, "output/output_data.json", pretty = TRUE)
Error in write_json(json_data, "output/output_data.json", pretty = TRUE) :
 could not find function "write_json"
>
> # 5. RDS FILES ----------------------------------
>
> # Save an R object to RDS
> saveRDS(csv_data, "output/data_object.rds")
Error: object 'csv_data' not found
>
> # Load it back
> loaded_data <- readRDS("output/data_object.rds")
In addition: Warning message:
In gzfile(file, "rb") :
 cannot open compressed file 'output/data_object.rds', probable reason 'No such file or directory'
> print("Loaded RDS Data:")
[1] "Loaded RDS Data:"
> print(head(loaded_data))
Write an R script to find subset of a dataset.
# Load the built-in mtcars dataset
data("mtcars")
# View the first few rows of the dataset
head(mtcars)
# Subset: Find cars with mpg (miles per gallon) greater than 20
subset_mpg_over_20 <- subset(mtcars, mpg > 20)
# Display the subset
print("Cars with mpg > 20:")
print(subset_mpg_over_20)
# Another example: Subset cars with 6 cylinders and hp greater than 100
subset_6cyl_hp <- subset(mtcars, cyl == 6 & hp > 100)
print("6-cylinder cars with horsepower > 100:")
print(subset_6cyl_hp)
# You can also select specific columns
subset_selected_columns <- subset(mtcars, mpg > 20, select = c(mpg, cyl, hp))
print("Subset with selected columns (mpg > 20):")
print(subset_selected_columns)
OUT-PUT
# Load the built-in mtcars dataset
> data("mtcars")
>
> # View the first few rows of the dataset
> head(mtcars)
           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
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant      18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
>
> # Subset: Find cars with mpg (miles per gallon) greater than 20
> subset_mpg_over_20 <- subset(mtcars, mpg > 20)
>
> # Display the subset
> print("Cars with mpg > 20:")
[1] "Cars with mpg > 20:"
> print(subset_mpg_over_20)
          mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4      21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710    22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Merc 240D      24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230        22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Fiat 128      32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Fiat X1-9     27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Volvo 142E      21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
>
> # Another example: Subset cars with 6 cylinders and hp greater than 100
> subset_6cyl_hp <- subset(mtcars, cyl == 6 & hp > 100)
>
> print("6-cylinder cars with horsepower > 100:")
[1] "6-cylinder cars with horsepower > 100:"
> print(subset_6cyl_hp)
            mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4        21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Valiant       18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Merc 280        19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C       17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Elucidate the process of data exploration in R using read (), summary (), nrow (), ncol (), str ().
# Read a CSV file (assumes 'data.csv' exists in your working directory)
data <- read.csv("data.csv")
# Show a summary of the dataset
summary(data)
# Show the number of rows
cat("Number of rows:", nrow(data), "\n")
# Show the number of columns
cat("Number of columns:", ncol(data), "\n")
# Show the structure of the dataset
str(data)
read.csv() Reads a CSV file into a data frame
summary() Gives a statistical summary (min, max, mean, etc.) for each column
nrow()        Returns the number of rows in the dataset
ncol()        Returns the number of columns in the dataset
str()         Shows the structure of the dataset, including data types and sample data