0% found this document useful (0 votes)
6 views14 pages

R-1ST Internal-Lab Notes

The document contains multiple R programming exercises demonstrating various data types, data structures, user input handling, operators, and matrix operations. It includes code snippets for numeric, integer, complex, logical, and character data types, as well as vectors, matrices, arrays, data frames, lists, and factors. Additionally, it covers arithmetic, relational, logical, assignment, and miscellaneous operators, and concludes with creating a 4x4 matrix and calculating its sum.

Uploaded by

sandhya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views14 pages

R-1ST Internal-Lab Notes

The document contains multiple R programming exercises demonstrating various data types, data structures, user input handling, operators, and matrix operations. It includes code snippets for numeric, integer, complex, logical, and character data types, as well as vectors, matrices, arrays, data frames, lists, and factors. Additionally, it covers arithmetic, relational, logical, assignment, and miscellaneous operators, and concludes with creating a 4x4 matrix and calculating its sum.

Uploaded by

sandhya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1st internals R- Programming Lab-Programs:

1.Write a R program that includes various data types in R.

# R Program to demonstrate various data types

# Numeric Data Type

num_var <- 42.5

cat("Numeric variable:", num_var, "\n")

# Integer Data Type

int_var <- as.integer(42)

cat("Integer variable:", int_var, "\n")

# Complex Data Type

complex_var <- 4 + 5i

cat("Complex variable:", complex_var, "\n")

# Logical Data Type

logical_var <- TRUE

cat("Logical variable:", logical_var, "\n")

# Character Data Type

char_var <- "Hello, R!"

cat("Character variable:", char_var, "\n")


OUTPUT:

Numeric variable: 42.5

Integer variable: 42

Complex variable: 4+5i

Logical variable: TRUE

Character variable: Hello, R!

1. Write a R program for different types of data structures in R

# R Program to demonstrate different data structures

# Vector

vector_var <- c(1, 2, 3, 4, 5)

cat("Vector:\n", vector_var, "\n\n")

# Matrix

matrix_var <- matrix(1:9, nrow = 3, ncol = 3)

cat("Matrix:\n")

print(matrix_var)

cat("\n")

# Array

array_var <- array(1:12, dim = c(3, 2, 2))


cat("Array:\n")

print(array_var)

cat("\n")

# Data Frame

data_frame_var <- data.frame(

ID = 1:3,

Name = c("Alice", "Bob", "Charlie"),

Age = c(25, 30, 35)

cat("Data Frame:\n")

print(data_frame_var)

cat("\n")

# List

list_var <- list(

Vector = vector_var,

Matrix = matrix_var,

DataFrame = data_frame_var

cat("List:\n")
print(list_var)

cat("\n")

# Factor

# Creating factor using factor()


fac = factor(c("Male", "Female", "Male”, "Male", "Female", "Male",
"Female"))
print(fac)

OUTPUT:

Vector:

12345

Matrix:

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

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

Data Frame:

ID Name Age

1 1 Alice 25
2 2 Bob 30

3 3 Charlie 35

List:

$Vector

[1] 1 2 3 4 5

$Matrix

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

$Data Frame

ID Name Age

1 1 Alice 25

2 2 Bob 30

3 3 Charlie 35

Factor:

Male, Female, Male, Male, Female, Male, Female

…………………………………………………………………………
…………………………………………………………
3.Write a R program to take input from the user (name, age,

address, city, state) and display the values. Also, print the

version of the R installation.

# Taking user inputs for name, age, address, city, and state

name <- redline (prompt = "Enter your name: ")

age <- as. integer (redline(prompt = "Enter your age:"))

address <- redline(prompt = "Enter your address: ")

city <- redline(prompt = "Enter your city: ")

state <- readline(prompt = "Enter your state: ")

# Display the entered values

cat("\nYour entered details are:\n")

cat("Name:", name, "\n")

cat("Age:", age, "\n")

cat("Address:", address, "\n")

cat("City:", city, "\n")

cat("State:", state, "\n")

# Print the version of R installation


cat("\nThe version of R installed on your system

is:\n")

cat(R.version.string, "\n")

OUTPUT:

Enter your name: John Doe

Enter your age: 25

Enter your address: 123 Main St

Enter your city: Springfield

Enter your state: IL

Your entered details are:

Name: John Doe

Age: 25

Address: 123 Main St

City: Springfield

State: IL

The version of R installed on your system is:


R version 4.1.1 (2021-08-10)

……………………………………………………

……………………………………………………

……………………………………..

4.Write a R program that includes different operators.

# Arithmetic Operators

num1 <- 10

num2 <- 5

cat("Arithmetic Operators:\n")

cat("Addition: ", num1 + num2, "\n")

cat("Subtraction: ", num1 - num2, "\n")

cat("Multiplication: ", num1 * num2, "\n")

cat("Division: ", num1 / num2, "\n")

cat("Exponentiation: ", num1 ^ num2, "\n") # num1 raised

to the power of num2

cat("Modulus (remainder): ", num1 %% num2, "\n") #

Remainder of division

cat("Integer Division: ", num1 %/% num2, "\n\n") # Integer

division result
# Relational Operators

cat("Relational Operators:\n")

cat("Is num1 equal to num2? ", num1 == num2, "\n")

cat("Is num1 not equal to num2? ", num1 != num2, "\n")

cat("Is num1 greater than num2? ", num1 > num2, "\n")

cat("Is num1 less than num2? ", num1 < num2, "\n")

cat("Is num1 greater than or equal to num2? ", num1 >=

num2, "\n")

cat("Is num1 less than or equal to num2? ", num1 <= num2,

"\n\n")

# Logical Operators

cat("Logical Operators:\n")

cat("AND (num1 > 5 and num2 < 10): ", (num1 > 5) &

(num2 < 10), "\n")

cat("OR (num1 > 5 or num2 > 10): ", (num1 > 5) | (num2 >

10), "\n")

cat("NOT (num1 > 5): ", !(num1 > 5), "\n\n")

# Assignment Operators

cat("Assignment Operators:\n")
x <- 20

y <- 30

cat("x assigned value: ", x, "\n")

cat("y assigned value: ", y, "\n")

x <- x + 5 # Correct way to increment in R

y <- y - 5 # Correct way to decrement in R

cat("x after x + 5: ", x, "\n")

cat("y after y - 5: ", y, "\n\n")

# Miscellaneous Operators

cat("Miscellaneous Operators:\n")

vec <- c(1, 2, 3, 4, 5)

cat("Indexing a vector (3rd element): ", vec[3], "\n")

cat("Check if 4 is in the vector: ", 4 %in% vec, "\n")

cat("Accessing all elements except 3rd element: ", vec[-3], "\

n")

OUTPUT:
Arithmetic Operators:

Addition: 15

Subtraction: 5

Multiplication: 50

Division: 2

Exponentiation: 100000

Modulus (remainder): 0

Integer Division: 2

Relational Operators:

Is num1 equal to num2? FALSE

Is num1 not equal to num2? TRUE

Is num1 greater than num2? TRUE

Is num1 less than num2? FALSE

Is num1 greater than or equal to num2? TRUE

Is num1 less than or equal to num2? FALSE

Logical Operators:

AND (num1 > 5 and num2 < 10): TRUE


OR (num1 > 5 or num2 > 10): TRUE

NOT (num1 > 5): FALSE

Assignment Operators:

x assigned value: 20

y assigned value: 30

x after x += 5: 25

y after y -= 5: 25

Miscellaneous Operators:

Indexing a vector (3rd element): 3

Check if 4 is in the vector: TRUE

Accessing all elements except 3rd element: 1 2 4 5

5.Write a R program to create and store an array of 4 × 4

matrix and calculate its sum.

# Create a 4 × 4 matrix

my_matrix <- matrix(1:16, nrow = 4, ncol = 4)

# Print the matrix


cat("4 × 4 Matrix:\n")

print(my_matrix)

# Calculate the sum of the matrix elements

matrix_sum <- sum(my_matrix)

# Print the sum

cat("\nSum of all elements in the matrix:",

matrix_sum, "\n")

Explanation:

1. Creating the Matrix: The matrix() function is

used to create a matrix. The argument 1:16

specifies the elements, and nrow and ncol

define the dimensions of the matrix (4 × 4).

2. Calculating the Sum: The sum() function

calculates the total of all elements in the matrix.

3. Printing Results: The cat() and print()

functions display the matrix and its sum.


OUTPUT:

4 × 4 Matrix:

[,1] [,2] [,3] [,4]

[1,] 1 5 9 13

[2,] 2 6 10 14

[3,] 3 7 11 15

[4,] 4 8 12 16

Sum of all elements in the matrix: 136

……………………………………………………

……………………………………………………

…………………………

You might also like