Assignments Of Class 1
Introduction to R Programming
Assignment 1: Install R and RStudio
Problem Statement:
1. Install R and RStudio on your system.
2. Verify the installation by running a simple R command.
Solution:
Step 1: Download R
- Visit CRAN R Website and download R based on your operating system (Windows, macOS, or Linux).
- Run the installer and follow the on-screen instructions.
Step 2: Download RStudio
- Visit RStudio Website and download RStudio.
- Install it by following the instructions.
Step 3: Verify Installation
- Open RStudio and type the following command in the Console:
print("R and RStudio are successfully installed!")
- If you see the output "R and RStudio are successfully installed!", the installation is complete.
Assignment 2: Declare and Print Variables
Problem Statement:
Write an R script to declare three variables (numeric, character, logical) and print their values and data types.
Solution:
Step 1: Declare Variables
num_var <- 50 # Numeric variable
char_var <- "Data Science" # Character variable
log_var <- TRUE # Logical variable
Step 2: Print Values
print(num_var) # Output: 50
print(char_var) # Output: "Data Science"
print(log_var) # Output: TRUE
Step 3: Print Data Types
print(class(num_var)) # Output: "numeric"
print(class(char_var)) # Output: "character"
print(class(log_var)) # Output: "logical"
Assignment 3: Arithmetic Operations in R
Problem Statement:
Perform basic arithmetic operations (addition, subtraction, multiplication, division, exponentiation) in R.
Solution:
a <- 20
b <- 5
# Performing Arithmetic Operations
print(a + b) # Addition: 25
print(a - b) # Subtraction: 15
print(a * b) # Multiplication: 100
print(a / b) # Division: 4
print(a^b) # Exponentiation: 20^5 = 3200000
Assignment 4: Create and Manipulate Vectors
Problem Statement:
Create a numeric vector of 5 elements and perform basic arithmetic operations.
Solution:
Step 1: Create a Vector
numbers <- c(10, 20, 30, 40, 50)
Step 2: Perform Arithmetic Operations
print(numbers + 5) # Add 5 to each element
print(numbers * 2) # Multiply each element by 2
print(numbers / 2) # Divide each element by 2
Step 3: Access Elements
print(numbers[2]) # Access 2nd element
print(numbers[1:3]) # Access first 3 elements
Assignment 5: Find Mean, Median, and Standard Deviation of a Vector
Problem Statement:
Write an R script to calculate the mean, median, and standard deviation of a given numeric vector.
Solution:
numbers <- c(15, 25, 35, 45, 55)
# Calculate Mean
mean_value <- mean(numbers)
print(mean_value) # Output: 35
# Calculate Median
median_value <- median(numbers)
print(median_value) # Output: 35
# Calculate Standard Deviation
sd_value <- sd(numbers)
print(sd_value) # Output: Standard deviation of the vector
Assignment 6: Create a Character Vector and Perform Operations
Problem Statement:
Create a character vector with three names and perform basic operations like concatenation and length checking.
Solution:
names <- c("Alice", "Bob", "Charlie")
# Print the vector
print(names)
# Concatenate names with a separator
concat_names <- paste(names, collapse=", ")
print(concat_names) # Output: "Alice, Bob, Charlie"
# Find the length of the vector
print(length(names)) # Output: 3
Assignment 7: Logical Operations in R
Problem Statement:
Write an R script to declare logical variables and perform basic logical operations.
Solution:
a <- TRUE
b <- FALSE
print(a & b) # AND operation: FALSE
print(a | b) # OR operation: TRUE
print(!a) # NOT operation: FALSE
Assignment 8: Generate a Sequence of Numbers
Problem Statement:
Generate a sequence of numbers from 1 to 50 with a step of 5.
Solution:
seq_numbers <- seq(1, 50, by=5)
# Print the sequence
print(seq_numbers) # Output: 1 6 11 16 21 26 31 36 41 46
Assignment 9: Create a Factor and Check its Levels
Problem Statement:
Create a factor variable from a given vector and check its levels.
Solution:
colors <- c("Red", "Blue", "Green", "Red", "Green")
# Convert to factor
color_factor <- factor(colors)
# Print factor levels
print(levels(color_factor)) # Output: "Blue" "Green" "Red"
Assignment 10: Use Built-in Functions in R
Problem Statement:
Use built-in functions to find the square root, absolute value, and round off a number in R.
Solution:
x <- -25.678
# Square Root
print(sqrt(abs(x))) # Output: Square root of 25.678
# Absolute Value
print(abs(x)) # Output: 25.678
# Round Off
print(round(x, 2)) # Output: -25.68
Conclusion
These 10 assignments introduce fundamental R concepts in a practical, step-by-step manner.
✅ Practice all assignments in RStudio
✅ Experiment with different values
✅ Try modifying the scripts to test your understanding

No comments:
Post a Comment