TECHNO INDIA UNIVERSITY
SESSION: - 2023-2025
NAME: -SHASWATI PAUL
YEAR: - 2ND
BATCH: -MCA 2C
STUDENT ID: - 231001271211
SUBJECT: - R Programming Lab
ASSIGNMENT 1
Develop R scripts to solve following problems:
1. Question 1:
What is the output when the following is entered at the R prompt?
'C' + 'D'
Error Message: Non-numeric argument to binary operator
How to resolve the error:
Corrected Code:
C <- "10"
D <- "20"
result <- as.numeric(C) + as.numeric(D)
print(result)
Output:
[1] 30
Question 2:
What happens when the following code is run in R?
x = 5. .9; x
Error Message: Incorrect use of the .. operator.
How to resolve the error:
Corrected Code:
x <- c(5, 6, 7, 8, 9)
x
Output:
[1] 5 6 7 8 9
Question 3:
What is the output of the following R command?
as.raw(20)
Output:
[1] 14
Question 4:
What happens when the following is executed in R?
month.names
Error Message: 'month.names' object not found. Execution halted
Solution:
o Corrected Code:
print(month.names)
o Output:
[1] "January" "February" "March" "April" "May" "June"
"July" "August" "September" "October" "November"
"December"
Question 5:
What is the output of the following R command?
0xabc
Output:
[1] 2748
Question 6:
What happens when the following is executed in R?
month.abb
Output:
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep"
"Oct" "Nov" "Dec"
Question 7:
What happens when the following is executed in R?
x20
Error Message: Object "x20" not found
Solution:
o Corrected Code:
x20 <- 100
print(x20)
o Output:
[1] 100
Question 8:
What is the output of the following R command?
getwd()
Output:
[1] "/home/compiler"
Question 9:
What happens when the following R command is executed?
setwd()
Error Message: Argument "dir" is missing, with no default.
Question 10:
What happens when the following code is executed?
(29,59,5)
Error Message: Unexpected ',' in "(29,"
Solution:
o Corrected Code:
c(29, 59, 5)
o Output:
[1] 29 59 5
2. Question 1: How can we change the default prompt in R?
To modify the default prompt in R, we can use the options() function. By default, the R
prompt is set to "R>". However, it can be customized by specifying a new value for the
prompt option.
Code Example:
options(prompt = "MyRPrompt > ")
Question 2: Determining the Type of an R Object
To identify the type of an R object, you can use the following functions:
1. class() – Determines the object's class (e.g., "numeric," "character").
2. typeof() – Reveals the internal type (e.g., "double," "integer").
3. mode() – Indicates the object's mode (e.g., "numeric," "character").
4. str() – Provides the structure of the object.
Example:
x <- 42
class(x) # Output: "numeric"
typeof(x) # Output: "double"
mode(x) # Output: "numeric"
str(x) # Output: num 42
Question 3: Changing the Class of an Object
1. Convert to Numeric: as.numeric(object)
2. Convert to Character: as.character(object)
3. Convert to Factor: as.factor(object)
4. Convert to Integer: as.integer(object)
5. Convert to Logical: as.logical(object)
6. Convert to Data Frame: as.data.frame(object)
7. Convert to Matrix: as.matrix(object)
8. Convert to List: as.list(object)
# Convert character to numeric
x <- "123"
x_numeric <- as.numeric(x)
# Convert numeric to character
x <- 123
x_char <- as.character(x)
# Convert character vector to factor
x <- c("apple", "banana")
x_factor <- as.factor(x)
# Convert numeric to integer
x <- 123.456
x_integer <- as.integer(x)
# Convert numeric to logical
x <- 1
x_logical <- as.logical(x)
# Convert matrix to data frame
x <- matrix(1:4, nrow=2)
x_df <- as.data.frame(x)
# Convert data frame to matrix
x <- data.frame(a=1:2, b=3:4)
x_matrix <- as.matrix(x)
# Convert numeric vector to list
x <- c(1, 2, 3)
x_list <- as.list(x)
Question 4: Define an imaginary number.
1.using the complex() function
z <- complex(real=0, imaginary=1)
print(z) # Output: 0+1i
2. Using the `i` Subscript for Complex Numbers
Z <- 1 + 2i
print(Z) # Output: 1 + 2i
3. Basic Operations with Complex Numbers
Addition:
Z1 <- 1 + 2i
Z2 <- -3 + 4i
sum_Z <- Z1 + Z2
print(sum_Z) # Output: -2 + 6i
Subtraction:
diff_Z <- Z1 - Z2
print(diff_Z) # Output: 4 - 2i
Multiplication:
product_Z <- Z1 * Z2
print(product_Z) # Output: -11 + 2i
Division:
quotient_Z <- Z1 / Z2
print(quotient_Z) # Output: -0.2 + 0.4i
4. Magnitude & Argument
Magnitude:
magnitude <- Mod(Z1)
print(magnitude) # Output: 2.236068
Argument:
argument <- Arg(Z1)
print(argument) # Output: 1.107149
5. To Change the Working Directory in R
1. Using setwd():
o To set the working directory to a specific path:
o setwd("/path/to/your/directory")
o To verify the current working directory:
o getwd()
2. Using Relative Paths:
o To move to the parent directory (one level up):
o setwd("..")
3. On Windows:
o Example for setting the working directory:
o setwd("C:/Users/YourName/Document/R")
4. On Mac and Linux:
o Example for setting the working directory:
o setwd("/Users/YourName/Document/R")
Check if a Number is Prime
# Check if the number is prime
# Take user input for the number
number <- as.integer(readline(prompt = "Enter a number to
check if it's prime: "))
# Check if the number is less than or equal to 1
if (number <= 1) {
cat(number, "is not a prime number.\n")
} else {
is_prime <- TRUE # Assume the number is prime
for (i in 2:(number - 1)) {
# Check for divisibility by numbers between 2 and (number
- 1)
if (number %% i == 0) {
is_prime <- FALSE # Not prime if divisible by any
number
break
}
}
# Output result
if (is_prime) {
cat(number, "is a prime number.\n")
} else {
cat(number, "is not a prime number.\n")
}
}
Output Example:
Enter a number to check if it's prime: 17
17 is a prime number
Generate Fibonacci Series
# Generate Fibonacci series
# Take user input for the number of terms
number <- as.integer(readline(prompt = "Enter the number of
terms to calculate the Fibonacci series: "))
# Initialize the Fibonacci series vector
fib <- numeric(number)
# Define the first two terms
if (number >= 1) fib[1] <- 0
if (number >= 2) fib[2] <- 1
# Calculate the Fibonacci series for subsequent terms
for (i in 3:number) {
fib[i] <- fib[i - 1] + fib[i - 2]
}
# Output the Fibonacci series
cat("Fibonacci series with", number, "terms:\n")
print(fib)
Output Example:
Enter the number of terms to calculate the Fibonacci series:
10
Fibonacci series with 10 terms:
[1] 0 1 1 2 3 5 8 13 21 34
Calculate Factorial Using a Loop
# Calculate Factorial using a Loop
# Take user input for the number
number <- as.integer(readline(prompt = "Enter a number to
calculate its factorial: "))
# Check if the number is non-negative
if (number < 0) {
cat("Factorial is not defined for negative numbers\n")
} else {
result <- 1 # Initialize the result variable
# Loop to calculate the factorial
for (i in 1:number) {
result <- result * i
}
# Output the result
cat("Factorial of", number, "is", result, "\n")
}
Output Example:
Enter a number to calculate its factorial: 5
Factorial of 5 is 120