0% found this document useful (0 votes)
3 views20 pages

R File 6th Sem

The document contains ten R programming exercises that cover various topics such as creating matrices, comparing data frames, accessing matrix elements, performing matrix operations, sorting vectors, merging lists, finding factors, generating Fibonacci numbers, and creating bar and pie charts. Each program includes code snippets and print statements to display the results. The exercises aim to demonstrate fundamental R programming concepts and data manipulation techniques.

Uploaded by

ririn79475
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)
3 views20 pages

R File 6th Sem

The document contains ten R programming exercises that cover various topics such as creating matrices, comparing data frames, accessing matrix elements, performing matrix operations, sorting vectors, merging lists, finding factors, generating Fibonacci numbers, and creating bar and pie charts. Each program includes code snippets and print statements to display the results. The exercises aim to demonstrate fundamental R programming concepts and data manipulation techniques.

Uploaded by

ririn79475
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/ 20

PROGRAM 1:

Write a R program to create an array of two 3x3 matrices each with 3


rows and 3 columns from two given two vectors.

print("Two vectors of different lengths:")


v1 = c(1,3,4,5)
v2 = c(10,11,12,13,14,15)
print(v1)
print(v2)
result = array(c(v1,v2),dim = c(3,3,2))
print("New array:")
print(result)

1
OUTPUT:

2
PROGRAM 2:

Write a R program to compare two data frames to find the row(s) in first
data frame that are not present in second data frame.

df_90 = data.frame(
"item" = c("item1", "item2", "item3"),
"Jan_sale" = c(12, 14, 12),
"Feb_sale" = c(11, 12, 15),
"Mar_sale" = c(12, 14, 15)
)
df_91 = data.frame(
"item" = c("item1", "item2", "item3"),
"Jan_sale" = c(12, 14, 12),
"Feb_sale" = c(11, 12, 15),
"Mar_sale" = c(12, 15, 18)
)
print("Original Dataframes:")
print(df_90)
print(df_91)
print("Row(s) in first data frame that are not present in
second data frame:")
print(setdiff(df_90,df_91))

3
OUTPUT:

4
PROGRAM 3;

Write a R program to access the element at 3rd column and 2nd row,
only the 3rd row and only the 4th column of a given matrix.

row_names = c("row1", "row2", "row3", "row4")


col_names = c("col1", "col2", "col3", "col4")
M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames =
list(row_names, col_names))
print("Original Matrix:")
print(M)
print("Access the element at 3rd column and 2nd row:")
print(M[2,3])
print("Access only the 3rd row:")
print(M[3,])
print("Access only the 4th column:")
print(M[,4])

5
OUTPUT:

PROGRAM 4:
6
Write a R program to create two 2x3 matrix and add, subtract, multiply
and divide the matrixes.

# Create two 2x3 matrixes.


m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
print("Matrix-1:")
print(m1)
m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
print("Matrix-2:")
print(m2)

result = m1 + m2
print("Result of addition")
print(result)

result = m1 - m2
print("Result of subtraction")
print(result)

result = m1 * m2
print("Result of multiplication")
print(result)

result = m1 / m2
print("Result of division:")
print(result)

OUTPUT:

7
PROGRAM 5:

8
Write a R program to sort a Vector in ascending and descending order.

x = c(10, 20, 30, 25, 9, 26)


print("Original Vectors:")
print(x)
print("Sort in ascending order:")
print(sort(x))
print("Sort in descending order:")
print(sort(x, decreasing=TRUE))

OUTPUT:

9
PROGRAM 6:

Write a R program to merge two given lists into one list.


10
n1 = list(1,2,3)
c1 = list("Red", "Green", "Black")
print("Original lists:")
print(n1)
print(c1)
print("Merge the said lists:")
mlist = c(n1, c1)
print("New merged list:")
print(mlist)

OUTPUT:

11
PROGRAM 7:

Write a R program to find the factors of a given number.

12
print_factors = function(n) {
print(paste("The factors of",n,"are:"))
for(i in 1:n) {
if((n %% i) == 0) {
print(i)
}
}
}
print_factors(4)
print_factors(7)
print_factors(12)

OUTPUT:

13
PROGRAM 8:

14
Write a R program to get the first 10 Fibonacci numbers.

Fibonacci <- numeric(10)


Fibonacci[1] <- Fibonacci[2] <- 1
for (i in 3:10) Fibonacci[i] <- Fibonacci[i - 2] + Fibonacci[i
- 1]
print("First 10 Fibonacci numbers:")
print(Fibonacci)

15
OUTPUT:

16
PROGRAM 9:

Write a R program to make chart using barplot.

# defining vector

x <- c(7, 15, 23, 12, 44, 56, 32)

# output to be present as PNG file

png(file = "barplot.png")

# plotting vector

barplot(x, xlab = "GeeksforGeeks Audience",

ylab = "Count", col = "white",

col.axis = "darkgreen",

col.lab = "darkgreen")

# saving the file

dev.off()

17
OUTPUT:

18
PROGRAM 10:
Write a R program to make a chart using piechart3D.

# importing library plotrix for pie3D()

library(plotrix)

# defining vector x with number of articles

x <- c(210, 450, 250, 100, 50, 90)

# defining labels for each value in x

names(x) <- c("Algo", "DS", "Java", "C", "C++", "Python")

# output to be present as PNG file

png(file = "piechart3d.png")

# creating pie chart

pie3D(x, labels = names(x), col = "white",

main = "Articles on GeeksforGeeks",

labelcol = "darkgreen", col.main = "darkgreen")

# saving the file

dev.off()

19
OUTPUT:

20

You might also like