[go: up one dir, main page]

0% found this document useful (0 votes)
7 views6 pages

R Programming

The document explains data structures in R, focusing on data frames, matrices, lists, and arrays. It highlights the differences between data frames and matrices, provides R code examples for creating and manipulating these structures, and demonstrates how to handle NULL and NA values in calculations. Additionally, it covers creating vectors and combining them into matrices, as well as creating and accessing elements in lists and arrays.

Uploaded by

saagarraj2580
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)
7 views6 pages

R Programming

The document explains data structures in R, focusing on data frames, matrices, lists, and arrays. It highlights the differences between data frames and matrices, provides R code examples for creating and manipulating these structures, and demonstrates how to handle NULL and NA values in calculations. Additionally, it covers creating vectors and combining them into matrices, as well as creating and accessing elements in lists and arrays.

Uploaded by

saagarraj2580
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/ 6

1. What is data frame in R.? How it is different from matrix? Illustrate with r code?

A data frame in R is a table or a two-dimensional array-like structure in which each column can
contain different types of data (numeric, character, factor, etc.). Data frames are used for storing
data tables and are one of the most used data structures in R for data analysis.

Differences between Data Frame and Matrix:

1. Type of Data:
o Data Frame: Can contain different types of data in different columns (numeric,
character, factor, etc.).
o Matrix: Can only contain one type of data (all elements must be of the same
type).
2. Column Names:
o Data Frame: Columns can have names.
o Matrix: Columns can have names, but it's less common to use them in practice.
3. Row Names:
o Data Frame: Rows can have names.
o Matrix: Rows can have names, but it's less common to use them in practice

Here's the R script to illustrate the differences:

# creating a data frame


df <- data. Frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Salary = c(50000, 60000, 70000)
)

# Display the data frame


print("Data Frame:")
print(df)

# Creating a matrix
mat <- matrix(
c(25, 30, 35, 50000, 60000, 70000),
nrow = 3,
ncol = 2,
byrow = TRUE
)

# Adding column and row names to the matrix


colnames(mat) <- c("Age", "Salary")
rownames(mat) <- c("Alice", "Bob", "Charlie")
# Display the matrix
print("Matrix:")
print(mat)

Output

[1] "Data Frame:"


Name Age Salary
1 Alice 25 50000
2 Bob 30 60000
3 Charlie 35 70000

2. Create a vector of length 5 that includes the numerical value with NULL
and NA value and compute Sum, Mean and Product of a Vector
First create a vector of length 5 that includes numerical values along with NULL and NA
values. Then, we'll compute the sum, mean, and product of the vector. We'll handle the
NULL and NA values appropriately in these calculations.

# Create a vector of length 5 including NULL and NA


vec <- c(1, 2, NULL, NA, 4)

# Compute Sum
sum_vec <- sum(vec, na.rm = TRUE)
print(paste("Sum:", sum_vec))

# Compute Mean
mean_vec <- mean(vec, na.rm = TRUE)
print(paste("Mean:", mean_vec))

# Compute Product
prod_vec <- prod(vec, na.rm = TRUE)
print(paste("Product:", prod_vec))

This will print the sum, mean, and product of the vector, handling NA values appropriately by
removing them during the calculations.
3. What is list in R? Create a list of seven days of a week and access the 3rd, 4th
and 5th elements using the index of the element?

In R, a list is a versatile data structure that can hold elements of different types, including
numbers, strings, vectors, and even other lists. Lists are particularly useful for storing collections
of heterogeneous data.

# Create a list of seven days of the week


days_of_week <- list("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday")

# Access the 3rd, 4th, and 5th elements using the index of the element
third_day <- days_of_week[[3]]
fourth_day <- days_of_week[[4]]
fifth_day <- days_of_week[[5]]

# Print the elements


print(paste("3rd day:", third_day))
print(paste("4th day:", fourth_day))
print(paste("5th day:", fifth_day))

Output
[1] "3rd day: Tuesday"
[1] "4th day: Wednesday"
[1] "5th day: Thursday"

4. Create three vectors x,y,z with integers and each vector has 3 elements.
Combine the three vectors to become a 3×3 matrix.
Sure, let's create three vectors x, y, and z, each with 3 integer elements. Then, we'll combine
these vectors to form a 3×3 matrix. Here's how you can do this in R:
# Create three vectors x, y, z with integers and each vector has 3 elements
x <- c(1, 2, 3)
y <- c(4, 5, 6)
z <- c(7, 8, 9)

# Combine the three vectors to become a 3x3 matrix using matrix()


function
combined_matrix_matrix <- matrix(c(x, y, z), nrow = 3, byrow = TRUE)
print("Matrix using matrix() function:")
print(combined_matrix_matrix)

# Combine the three vectors to become a 3x3 matrix using cbind()


function
combined_matrix_cbind <- cbind(x, y, z)
print("Matrix using cbind() function:")
print(combined_matrix_cbind)

# Combine the three vectors to become a 3x3 matrix using rbind()


function
combined_matrix_rbind <- rbind(x, y, z)
print("Matrix using rbind() function:")
print(combined_matrix_rbind)

Output:

Running the script will produce the following matrices:

 Matrix using matrix() function:

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


[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

Matrix using cbind() function:


xyz
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Matrix using rbind() function:
[,1] [,2] [,3]
x 1 2 3
y 4 5 6
z 7 8 9

5. Differential array with matrix? Create an array (3 dimensional) of 24 elements and


Print the matrix of third dimension.

In R, an array is a multi-dimensional data structure that can store data in two or more
dimensions. To create an array with three dimensions and 24 elements, we can use the array
() function. We'll then extract and print the matrix of the third dimension.
# Create an array with 3 dimensions and 24 elements
array_3d <- array(1:24, dim = c(4, 3, 2))

# Print the entire array to understand its structure


print("3D Array:")
print(array_3d)

# Access and print the matrix of the second slice (third dimension)
third_dimension_matrix <- array_3d[, , 2]
print("Matrix of the second slice (third dimension):")
print(third_dimension_matrix)

Output:

3D Array:
,,1

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


[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

,,2

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


[1,] 13 17 21
[2,] 14 18 22
[3,] 15 19 23
[4,] 16 20 24

Matrix of the second slice (third dimension):


[,1] [,2] [,3]
[1,] 13 17 21
[2,] 14 18 22
[3,] 15 19 23
[4,] 16 20 24

You might also like