R programming lab programs
R programming lab programs
# Vector
vector_example<- c(1, 2, 3, 4, 5)
print("Vector:")
print(vector_example)
# List
list_example<- list(name = "John", age = 30, city = "New York")
print("List:")
print(list_example)
# Matrix
matrix_example<- matrix(1:9, nrow = 3, ncol = 3)
print("Matrix:")
print(matrix_example)
# Data Frame
data_frame_example<- data.frame(
name = c("John", "Alice", "Bob"),
age = c(30, 25, 35),
city = c("New York", "Los Angeles", "Chicago")
)
print("Data Frame:")
print(data_frame_example)
# Factor
factor_example<- factor(c("Male", "Female", "Male", "Female", "Male"))
print("Factor:")
print(factor_example)
# Array
array_example<- array(1:12, dim = c(3, 2, 2))
print("Array:")
print(array_example)
output:-
[1] Vector:
[1] 1 2 3 4 5
[1] List:
$name
[1] "John"
$age
[1] 30
$city
[1] New York
[1] Matrix:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] Data Frame:
name age city
1 John 30 New York
2 Alice 25 Los Angeles
3 Bob 35 Chicago
[1] Factor:
[1] Male Female Male Female Male
Levels: Female Male
[1] Array:
[ , , 1]
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
[ , , 2]
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
2) Write a R program that include variables, constants, and data types.
# Constants
PI <- 3.14159
GRAVITY <- 9.81
# Variables
name <- "John"
age <- 30
height <- 175.5
is_student<- TRUE
print("Variables:")
print(name)
print(age)
print(height)
print(is_student)
# Data types
print("Data types:")
print(typeof(name)) # Character
print(typeof(age)) # Integer
print(typeof(height)) # Double
print(typeof(is_student)) # Logical
Output:-
[1] Constants:
[1] 3.14159
[1] 9.81
[1] Variables:
[1] "John"
[1] 30
[1] 175.5
[1] TRUE
[1] "Data types:"
[1] "character"
[1] "double"
[1] "double"
[1] "logical"
3) Write a R program that include different operators, control structures,
default values for arguments, returning complex objects.
# Calculate perimeter
perimeter <- 2 * (length + width)
Output:-
[1] Rectangle 1:
$area
[1] 5
$perimeter
[1] 12
[1] Rectangle 2:
$area
[1] 21
$perimeter
[1] 20
[1] "10 is Even"
[1] "7 is Odd"
4) Write a R program for calculating cumulative sums, and products
minima maxima and calculus.
Output:-
[1] Cumulative Sum:
[1] 1 3 6 10 15
[1] Cumulative Product:
[1] 1 2 6 24 120
[1] Minimum:
[1] 1
[1] Maximum:
[1] 5
[1] Derivative of the quadratic function at x = 2:
[1] 6.000001
5) Write a R program that include linear algebra operations on vectors
and matrices.
Output:-
[1] Vector Addition:
[1] 5 7 9