0% found this document useful (0 votes)
60 views

R Exam

The document demonstrates various R programming concepts like vectors, matrices, data frames, and lists. It shows how to perform basic operations on these objects like addition, subtraction, extraction of elements. It also shows creating and manipulating different data structures and performing operations on them like subsetting, adding columns etc.

Uploaded by

amu j
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)
60 views

R Exam

The document demonstrates various R programming concepts like vectors, matrices, data frames, and lists. It shows how to perform basic operations on these objects like addition, subtraction, extraction of elements. It also shows creating and manipulating different data structures and performing operations on them like subsetting, adding columns etc.

Uploaded by

amu j
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/ 18

1.

u <- 4
v <- 8
u + v
## [1] 12
u - v
## [1] -4
u * v

## [1] 32
u / v
## [1] 0.5
u^v
## [1] 65536

7.
a = c(1, 2, 5, 3, 4, 0, -1, -3)
b = c("Red", "Green", "White")
c = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
print(a)
print(typeof(a))
print(b)
print(typeof(b))
print(c)
print(typeof(c))

Sample Output:

[1] 1 2 5 3 4 0 -1 -3
[1] "double"
[1] "Red" "Green" "White"
[1] "character"
[1] TRUE TRUE TRUE FALSE TRUE FALSE
[1] "logical"

8.
m1 = matrix(1:20, nrow=5, ncol=4)
print("5 × 4 matrix:")
print(m1)
cells = c(1,3,5,7,8,9,11,12,14)
rnames = c("Row1", "Row2", "Row3")
cnames = c("Col1", "Col2", "Col3")
m2 = matrix(cells, nrow=3, ncol=3, byrow=TRUE,
dimnames=list(rnames, cnames))
print("3 × 3 matrix with labels, filled by rows: ")
print(m2)
print("3 × 3 matrix with labels, filled by columns: ")
m3 = matrix(cells, nrow=3, ncol=3, byrow=FALSE,
dimnames=list(rnames, cnames))
print(m3)

Sample Output:

[1] "5 × 4 matrix:"


[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
[1] "3 × 3 matrix with labels, filled by rows: "
Col1 Col2 Col3
Row1 1 3 5
Row2 7 8 9
Row3 11 12 14
[1] "3 × 3 matrix with labels, filled by columns: "
Col1 Col2 Col3
Row1 1 7 11
Row2 3 8 12
Row3 5 9 14

9.
a = array(
6:30,
dim = c(4, 3, 2),
dimnames = list(
c("Col1", "Col2", "Col3", "Col4"),
c("Row1", "Row2", "Row3"),
c("Part1", "Part2")
)
)
print(a)

Sample Output:

, , Part1

Row1 Row2 Row3


Col1 6 10 14
Col2 7 11 15
Col3 8 12 16
Col4 9 13 17

, , Part2

Row1 Row2 Row3


Col1 18 22 26
Col2 19 23 27
Col3 20 24 28
Col4 21 25 29

10.
marks = c(70, 95, 80, 74)
barplot(marks,
main = "Comparing marks of 5 subjects",
xlab = "Marks",
ylab = "Subject",
names.arg = c("English", "Science", "Math.", "Hist."),
col = "darkred",
horiz = FALSE)

Sample Output:
11.
nums = c(10, 20, 30)
print('Original vector:')
print(nums)
print(paste("Sum of vector elements:",sum(nums)))
print(paste("Mean of vector elements:",mean(nums)))
print(paste("Product of vector elements:",prod(nums)))

Sample Output:

[1] "Original vector:"


[1] 10 20 30
[1] "Sum of vector elements: 60"
[1] "Mean of vector elements: 20"
[1] "Product of vector elements: 6000"

12.
Employees = data.frame(Name=c("Anastasia S","Dima
R","Katherine S", "JAMES A","LAURA MARTIN"),
Gender=c("M","M","F","F","M"),
Age=c(23,22,25,26,32),
Designation=c("Clerk","Manager","Exective","CEO","ASSIS
TANT"),
SSN=c("123-34-2346","123-44-
779","556-24-433","123-98-987","679-77-576")
)
print("Details of the employees:")
print(Employees)

Sample Output:

[1] "Details of the employees:"


Name Gender Age Designation SSN
1 Anastasia S M 23 Clerk 123-34-2346
2 Dima R M 22 Manager 123-44-779
3 Katherine S F 25 Exective 556-24-433
4 JAMES A F 26 CEO 123-98-987
5 LAURA MARTIN M 32 ASSISTANT 679-77-576

13.
data = women
print("Women data set of height and weights:")
print(data)
height_f = cut(women$height,3)
print("Factor corresponding to height:")
print(table(height_f))

Sample Output:

[1] "Women data set of height and weights:"


height weight
1 58 115
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
7 64 132
8 65 135
9 66 139
10 67 142
11 68 146
12 69 150
13 70 154
14 71 159
15 72 164
[1] "Factor corresponding to height:"
height_f
(58,62.7] (62.7,67.3] (67.3,72]
5 5 5

14.
v = c(1, 2, 3, 3, 4, NA, 3, 2, 4, 5, NA, 5)
print("Original vector:")
print(v)
print("Levels of factor of the said vector:")
print(levels(factor(v)))

Sample Output:

[1] "Original vector:"


[1] 1 2 3 3 4 NA 3 2 4 5 NA 5
[1] "Levels of factor of the said vector:"
[1] "1" "2" "3" "4" "5"

15.
list_data <- list(c("Red","Green","Black"),
matrix(c(1,3,5,7,9,11), nrow = 2),
list("Python", "PHP", "Java"))
print("List:")
print(list_data)
names(list_data) = c("Color", "Odd numbers",
"Language(s)")
print("List with column names:")
print(list_data)
print('1st element:')
print(list_data[1])
print('2nd element:')
print(list_data[2])

Sample Output:

[1] "List:"
[[1]]
[1] "Red" "Green" "Black"
[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

[[3]]
[[3]][[1]]
[1] "Python"

[[3]][[2]]
[1] "PHP"

[[3]][[3]]
[1] "Java"

[1] "List with column names:"


$Color
[1] "Red" "Green" "Black"

$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"

$`Language(s)`[[2]]
[1] "PHP"

$`Language(s)`[[3]]
[1] "Java"

[1] "1st element:"


$Color
[1] "Red" "Green" "Black"
[1] "2nd element:"
$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

16.
df1 = data.frame(y1 = c(0, 1, 2), y2 = c(3, 4, 5))
df2 = data.frame(y1 = c(6, 7, 8), y2 = c(9, 10, 11))
new_list = list(df1, df2)
print("New list:")
print(new_list)
print("Data frame-1")
print(new_list[[1]])
print("Data frame-2")
print(new_list[[2]])

Sample Output:

[1] "New list:"


[[1]]
y1 y2
1 0 3
2 1 4
3 2 5

[[2]]
y1 y2
1 6 9
2 7 10
3 8 11

[1] "Data frame-1"


y1 y2
1 0 3
2 1 4
3 2 5
[1] "Data frame-2"
y1 y2
1 6 9
2 7 10
3 8 11

17.
# 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)

Sample Output:

[1] "Matrix-1:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "Matrix-2:"
[,1] [,2] [,3]
[1,] 0 2 0
[2,] 1 3 2
[1] "Result of addition"
[,1] [,2] [,3]
[1,] 1 5 5
[2,] 3 7 8
[1] "Result of subtraction"
[,1] [,2] [,3]
[1,] 1 1 5
[2,] 1 1 4
[1] "Result of multiplication"
[,1] [,2] [,3]
[1,] 0 6 0
[2,] 2 12 12
[1] "Result of division:"
[,1] [,2] [,3]
[1,] Inf 1.500000 Inf
[2,] 2 1.333333 3

18.

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)
result = M[M[,3] > 7,]
print("New submatrix:")
print(result)

Sample Output:

[1] "Original Matrix:"


col1 col2 col3 col4
row1 1 2 3 4
row2 5 6 7 8
row3 9 10 11 12
row4 13 14 15 16
[1] "New submatrix:"
col1 col2 col3 col4
row3 9 10 11 12
row4 13 14 15 16
19.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James',
'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes')
)
print("Original dataframe:")
print(exam_data)
print("Extract 3rd and 5th rows with 1st and 3rd
columns :")
result = exam_data[c(3,5),c(1,3)]
print(result)

Sample Output:

[1] "Original dataframe:"


name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
[1] "Extract 3rd and 5th rows with 1st and 3rd
columns :"
name attempts
3 Katherine 2
5 Emily 2

20.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James',
'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes')
)
print("Original dataframe:")
print(exam_data)
print("New data frame after adding the 'country'
column:")
exam_data$country =
c("USA","USA","USA","USA","USA","USA","USA","USA","USA"
,"USA")
print(exam_data)

Sample Output:

[1] "Original dataframe:"


name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
[1] "New data frame after adding the 'country' column:"
name score attempts qualify country
1 Anastasia 12.5 1 yes USA
2 Dima 9.0 3 no USA
3 Katherine 16.5 2 yes USA
4 James 12.0 3 no USA
5 Emily 9.0 2 no USA
6 Michael 20.0 3 yes USA
7 Matthew 14.5 1 yes USA
8 Laura 13.5 1 no USA
9 Kevin 8.0 2 no USA
10 Jonas 19.0 1 yes USA

21.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James',
'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes',
'yes', 'no', 'no', 'yes')
)
print("Original dataframe:")
print(exam_data)
new_exam_data = data.frame(
name = c('Robert', 'Sophia'),
score = c(10.5, 9),
attempts = c(1, 3),
qualify = c('yes', 'no')
)
exam_data = rbind(exam_data, new_exam_data)
print("After adding new row(s) to an existing data
frame:")
print(exam_data)

Sample Output:

[1] "Original dataframe:"


name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
[1] "After adding new row(s) to an existing data
frame:"
name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
11 Robert 10.5 1 yes
12 Sophia 9.0 3 no

22.
data = airquality
print("Original data: Daily air quality measurements in
New York, May to September 1973.")
print(class(data))
print(head(data,10))
result = data[order(data[,1]),]
print("Order the entire data frame by the first and
second column:")
print(result)

Sample Output:

[1] "Original data: Daily air quality measurements in


New York, May to September 1973."
[1] "data.frame"
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
10 NA 194 8.6 69 5 10
[1] "Order the entire data frame by the first and
second column:"
Ozone Solar.R Wind Temp Month Day
21 1 8 9.7 59 5 21
23 4 25 9.7 61 5 23
18 6 78 18.4 57 5 18
...........
119 NA 153 5.7 88 8 27
150 NA 145 13.2 77 9 27

23.
array1 = array(1:30, dim=c(3,5,2))
print(array1)

Sample Output:

, , 1

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


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

, , 2

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


[1,] 16 19 22 25 28
[2,] 17 20 23 26 29
[3,] 18 21 24 27 30

24.
num1 = rbind(rep("A",3), rep("B",3), rep("C",3))
print("num1")
print(num1)
num2 = rbind(rep("P",3), rep("Q",3), rep("R",3))
print("num2")
print(num2)
num3 = rbind(rep("X",3), rep("Y",3), rep("Z",3))
print("num3")
print(num3)
a = matrix(t(cbind(num1,num2,num3)),ncol=3, byrow=T)
print("Combine three arrays, taking one row from each
one by one:")
print(a)

Sample Output:

[1] "num1"
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "B" "B" "B"
[3,] "C" "C" "C"
[1] "num2"
[,1] [,2] [,3]
[1,] "P" "P" "P"
[2,] "Q" "Q" "Q"
[3,] "R" "R" "R"
[1] "num3"
[,1] [,2] [,3]
[1,] "X" "X" "X"
[2,] "Y" "Y" "Y"
[3,] "Z" "Z" "Z"
[1] "Combine three arrays, taking one row from each one
by one:"
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "P" "P" "P"
[3,] "X" "X" "X"
[4,] "B" "B" "B"
[5,] "Q" "Q" "Q"
[6,] "Y" "Y" "Y"
[7,] "C" "C" "C"
[8,] "R" "R" "R"
[9,] "Z" "Z" "Z"

25.
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)
print("The second row of the second matrix of the
array:")
print(result[2,,2])
print("The element in the 3rd row and 3rd column of the
1st matrix:")
print(result[3,3,1])

Sample Output:

[1] "Two vectors of different lengths:"


[1] 1 3 4 5
[1] 10 11 12 13 14 15
[1] "New array:"
, , 1

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


[1,] 1 5 12
[2,] 3 10 13
[3,] 4 11 14

, , 2

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


[1,] 15 4 11
[2,] 1 5 12
[3,] 3 10 13

[1] "The second row of the second matrix of the array:"


[1] 1 5 12
[1] "The element in the 3rd row and 3rd column of the
1st matrix:"
[1] 14

You might also like