G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016
BCA I SEM I
SBC4PI105AVS
PRACTICAL LIST-R Programming
R1. Write a program in R programming language to print “ Hello World”.
Solution:
# Program to print Hello World
print("Hello, World!")
Output:
[1] "Hello, World!"
R2. Write a program in R programming language to add two numbers.
Solution:
# Adding two numbers
a <- 10
b <- 20
sum <- a + b
print(paste("Sum =", sum))
Output:
[1] "Sum = 30"
R3. Write a program in R programming language to find the square and cube of a number.
Solution:
num <- 5
square <- num^2
cube <- num^3
print(paste("Square =", square))
print(paste("Cube =", cube))
Output
[1] "Square = 25"
[1] "Cube = 125"
R4. Write a program in R programming language to check if a number is even or odd.
Solution:
num <- 9
if (num %% 2 == 0) {
print("Even Number")
} else {
print("Odd Number")
}
Output
[1] "Odd Number"
G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016
R5. Write a program in R programming language to generate a sequence of numbers.
Solution:
# Sequence from 1 to 10
seq_num <- seq(1, 10, by = 1)
print(seq_num)
Output:
[1] 1 2 3 4 5 6 7 8 9 10
R6. Write a program in R programming language to create vectors.
Solution:
# Creating a vector
v <- c(10, 20, 30, 40, 50)
print(v)
# Vector operations
print(sum(v)) # Sum
print(mean(v)) # Average
print(max(v)) # Maximum
print(min(v)) # Minimum
Output:
[1] 10 20 30 40 50
[1] 150
[1] 30
[1] 50
[1] 10
R7. Write a program in R programming language using built-in functions.
Solution:
x <- c(2, 4, 6, 8, 10)
print(length(x)) # Number of elements
print(sd(x)) # Standard deviation
print(var(x)) # Variance
Output:
[1] 5
[1] 3.162278
[1] 10
R8. Write a program in R programming language to plot a simple graph.
# Simple Line Plot
x <- 1:10
y <- x^2
plot(x, y, type="o", col="blue", main="Simple Line
Plot", xlab="X Values", ylab="Y = X^2")
Output
G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016
R9. Write a program in R programming language to create variables of different data types and display their types.
Solution:
# Numeric
num_var <- 42.5
print(paste("Type of num_var:", class(num_var)))
# Integer
int_var <- 10L
print(paste("Type of int_var:", class(int_var)))
# Character
char_var <- "Hello World"
print(paste("Type of char_var:", class(char_var)))
# Logical
bool_var <- TRUE
print(paste("Type of bool_var:", class(bool_var)))
# Complex
complex_var <- 3 + 4i
print(paste("Type of complex_var:", class(complex_var)))
Output:
G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016
R10. Write a program in R programming language to perform basic arithmetic operations on two numbers.
Solution:
a <- 15
b <- 4
addition <- a + b
subtraction <- a - b
multiplication <- a * b
division <- a / b
modulus <- a %% b
power <- a ^ b
cat("Addition:", addition, "\n")
cat("Subtraction:", subtraction, "\n")
cat("Multiplication:", multiplication, "\n")
cat("Division:", division, "\n")
cat("Modulus:", modulus, "\n")
cat("Power:", power, "\n")
Output:
R11. Write a program in R programming language to create vectors and perform various operations.
Solution:
# Create vectors
vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c(10, 20, 30, 40, 50)
# Vector operations
sum_vec <- vec1 + vec2
product_vec <- vec1 * vec2
length_vec <- length(vec1)
mean_vec <- mean(vec1)
max_vec <- max(vec1)
min_vec <- min(vec1)
print("Vector 1:")
print(vec1)
G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016
print("Vector 2:")
print(vec2)
print("Sum of vectors:")
print(sum_vec)
print("Product of vectors:")
print(product_vec)
cat("Length:", length_vec, "Mean:", mean_vec, "Max:", max_vec, "Min:", min_vec)
R12. Write a program in R programming language to plot basic scatter plot (Miles per Gallon vs Horsepower).
Solution:
plot(mtcars$hp, mtcars$mpg,
main = "Miles per Gallon vs Horsepower",
xlab = "Horsepower",
ylab = "Miles per Gallon",
col = "blue",
pch = 19)
G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016
R13. Write a program in R programming language to plot line graph (MPG of Cars by Weight)
Solution:
plot(mtcars$wt, mtcars$mpg, type = "o",
main = "MPG vs Weight",
xlab = "Weight (1000 lbs)",
ylab = "Miles per Gallon",
col = "darkgreen")
Output:
R14. Write a program in R programming language to plot box plot(MPG Grouped by Number of Cylinders)
Solution:
boxplot(mpg ~ cyl, data = mtcars,
main = "MPG by Number of Cylinders",
xlab = "Number of Cylinders",
ylab = "Miles per Gallon",
col = c("orange", "lightblue", "lightgreen"))
Output:
G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016
R15. Write a program in R programming language to create bar plot (Average Horsepower by Cylinder Count)
Solution:
avg_hp <- tapply(mtcars$hp, mtcars$cyl, mean)
barplot(avg_hp,
main = "Average Horsepower by Cylinder Count",
xlab = "Cylinders",
ylab = "Average Horsepower",
col = "purple")
R156 Write a program in R programming language to create histogram(Distribution of Miles per Gallon)
Solution:
hist(mtcars$mpg,
main = "Distribution of Miles per Gallon",
xlab = "Miles per Gallon",
col = "skyblue",
border = "white")
G H RAISONI SKILLTECH UNIVERSITY, NAGPUR
B-37/39-1, SHRADDHA PARK, HARIGANGA CAMPUS, HINGNA WADI LINK ROAD, MIDC NAGPUR 440016