[go: up one dir, main page]

0% found this document useful (0 votes)
35 views22 pages

R Programming Manual

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

R Programming Manual

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

LAB MANUAL

FOR

R PROGRAMMING

LABORATORY

II B. TECH II SEMESTER (JNTUK-R20)

NEWTON’S INSTITUTE OF ENGINEERING COLLEGE


ALUGURAJUPALLI, KOPPUNOOR, MACHERLA, PALNADU, 522426

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


1) Write a R program to take input from the user (name and age) and display the
values. Also print the version of R installation.

PROGRAM:
name = readline(prompt="Input your name: ")
age = readline(prompt="Input your age: ")
print(paste("My name is",name, "and I am",age ,"years old."))
print(R.version.string)

OUTPUT:
name = readline(prompt="Input your name: ")
Input your name: NARESH
> age = readline(prompt="Input your age: ")
Input your age: 42
> print(paste("My name is",name, "and I am",age ,"years old."))
[1] "My name is NARESH and I am 42 years old."
> print(R.version.string)
[1] "R version 4.2.2 Patched (2022-11-10 r83330)"
>

2) Write a R program to get the details of the objects in memory.

PROGRAM:
name = "Python";
n1 = 10;
n2 = 0.5
nums = c(10, 20, 30, 40, 50, 60)
print(ls())
print("Details of the objects in memory:")
print(ls.str())

output:
[1] "n1" "n2" "name" "nums"
[1] "Details of the objects in memory:"
n1 : num 10
n2 : num 0.5
name : chr "Python"
nums : num [1:6] 10 20 30 40 50 60

Page 1
3) Write a R program to create a sequence of numbers from 20 to 50 and find the
mean of numbers from 20 to 60 and sum of numbers from 51 to 91.
PROGRAM:
print("Sequence of numbers from 20 to 50:")
print(seq(20,50))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))

OUTPUT:
[1] "Sequence of numbers from 20 to 50:"
[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
45 46 47 48 49 50
[1] "Mean of numbers from 20 to 60:"
[1] 40
[1] "Sum of numbers from 51 to 91:"
[1] 2911

4) Write a R program to create a simple bar plot of five subjects marks.

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

Page 2
5) Write a R program to get the unique elements of a given string and unique
numbers of vector.
PROGRAM:
str1 = "The quick brown fox jumps over the lazy dog."
print("Original vector(string)")
print(str1)
print("Unique elements of the said vector:")
print(unique(tolower(str1)))
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
print("Original vector(number)")
print(nums)
print("Unique elements of the said vector:")
print(unique(nums))
output:
[1] "Original vector(string)"
[1] "The quick brown fox jumps over the lazy dog."
[1] "Unique elements of the said vector:"
[1] "the quick brown fox jumps over the lazy dog."
[1] "Original vector(number)"
[1] 12234456
[1] "Unique elements of the said vector:"
[1] 123456

6) Write a R program to create three vectors a,b,c with 3 integers. Combine the
three vectors to become a 3×3 matrix where each column represents a vector.
Print the content of the matrix.
PROGRAM:
a<-c(1,2,3)
b<-c(4,5,6)
c<-c(7,8,9)
m<-cbind(a,b,c)
print("Content of the said matrix:")
print(m)
OUT PUT:
[1] "Content of the said matrix:"
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

Page 3
7) Write a R program to create a 5 x 4 matrix , 3 x 3 matrix with labels and fill the
matrix by rows and 2 × 2 matrix with labels and fill the matrix by columns.

PROGRAM:
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)

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
Page 4
8) Write a R program to combine three arrays so that the first row of the first array
is followed by the first row of the second array and then first row of the third
array.
PROGRAM:
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)
Out put:
num1 = rbind(rep("A",3), rep("B",3), rep("C",3))
> print("num1")
[1] "num1"
> print(num1)
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "B" "B" "B"
[3,] "C" "C" "C"
> num2 = rbind(rep("P",3), rep("Q",3), rep("R",3))
> print("num2")
[1] "num2"
> print(num2)
[,1] [,2] [,3]
[1,] "P" "P" "P"
[2,] "Q" "Q" "Q"
[3,] "R" "R" "R"
> num3 = rbind(rep("X",3), rep("Y",3), rep("Z",3))
> print("num3")
[1] "num3"
> print(num3)
[,1] [,2] [,3]
[1,] "X" "X" "X"
[2,] "Y" "Y" "Y"
[3,] "Z" "Z" "Z"
> a = matrix(t(cbind(num1,num2,num3)),ncol=3, byrow=T)
> print("Combine three arrays, taking one row from each one by one:")
[1] "Combine three arrays, taking one row from each one by one:"
> print(a)
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "P" "P" "P"
[3,] "X" "X" "X"

Page 5
[4,] "B" "B" "B"
[5,] "Q" "Q" "Q"
[6,] "Y" "Y" "Y"
[7,] "C" "C" "C"
[8,] "R" "R" "R"
[9,] "Z" "Z" "Z"
>

9) Write a R program to create a two-dimensional 5x3 array of sequence of even


integers greater than 50.
PROGRAM:

a <- array(seq(from = 50, length.out = 15, by = 2), c(5, 3))


print("Content of the array:")
print("5×3 array of sequence of even integers greater than 50:")
print(a)

output:
>a <- array(seq(from = 50, length.out = 15, by = 2),c(5, 3))
> print("Content of the array:")
[1] "Content of the array:"
> print("5×3 array of sequence of even integers greater than 50:")
[1] "5×3 array of sequence of even integers greater than 50:"
> print(a)
[ ,1] [,2] [,3]

[1,] 50 60 70

[2,] 52 62 72

[3,] 54 64 74

[4,] 56 66 76

[5,] 58 68 78
>

Page 6
10) Write a R program to create an array using four given columns, three given
rows, and two given tables and display the content of the array.

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

output:
> array1 = array(1:30, dim=c(3,5,2))
> print(array1)
,,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

11) Write a R program to create an empty data frame.


Solution:
df = data.frame(Ints=integer(),
Doubles=double(),
Characters=character(),
Logicals=logical(),
Factors=factor(),
stringsAsFactors=FALSE)
print("Structure of the empty dataframe:")
print(str(df))

output:
"Structure of the empty dataframe:"
'data.frame': 0 obs. of 5 variables:
$ Ints : int
$ Doubles : num
$ Characters: chr
$ Logicals : logi
$ Factors : Factor w/ 0 levels:
NULL

Page 7
12) Write a R program to create a data frame from four given vectors.

Solution:
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 data frame:")
print(name)
print(score)
print(attempts)
print(qualify)
df = data.frame(name, score, attempts, qualify)
print(df)
output:
[1] "Original data frame:"
[1] "Anastasia" "Dima" "Katherine" "James" "Emily" "Michael"
[7] "Matthew" "Laura" "Kevin" "Jonas"
[1] 12.5 9.0 16.5 12.0 9.0 20.0 14.5 13.5 8.0 19.0
[1] 1 3 2 3 2 3 1 1 2 1
[1] "yes" "no" "yes" "no" "no" "yes" "yes" "no" "no" "yes"
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

Page 8
13) Write a R program to create a data frame using two given vectors and display
the duplicated elements and unique rows of the said data frame.

PROGRAM:
a = c(10,20,10,10,40,50,20,30)
b = c(10,30,10,20,0,50,30,30)
print("Original data frame:")
ab = data.frame(a,b)
print(ab)
print("Duplicate elements of the said data frame:")
print(duplicated(ab))
print("Unique rows of the said data frame:")
print(unique(ab))

output:
[1] "Original data frame:"
a b
1 10 10
2 20 30
3 10 10
4 10 20
5 40 0
6 50 50
7 20 30
8 30 30

[1] "Duplicate elements of the said data frame:"

[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE

[1] "Unique rows of the said data frame:"


a b
1 10 10
2 20 30
4 10 20
5 40 0
6 50 50
8 30 30

Page 9
14) Write a R program to save the information of a data frame in a file and display
the information of the file.
PROGRAM:
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)
save(exam_data,file="data.rda")
load("data.rda")
file.info("data.rda")

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

size isdir mode mtime ctime


data.rda 344 FALSE 644 2018-10-25 12:06:09 2018-10-25 12:06:09
atime uid gid uname grname
data.rda 2018-10-25 12:06:09 1000 1000 trinket trinket

Page 10
15) Write a R program to create a matrix from a list of given vectors.
PROGRAM:
l = list()
for (i in 1:5) l[[i]] <- c(i, 1:4)
print("List of vectors:")
print(l)
result = do.call(rbind, l)
print("New Matrix:")
print(result)
output:
[1] "List of vectors:"
[[1]]
[1] 1 1 2 3 4

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

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

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

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

[1] "New Matrix:"


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

[1,] 1 1 2 3 4

[2,] 2 1 2 3 4

[3,] 3 1 2 3 4

[4,] 4 1 2 3 4

[5,] 5 1 2 3 4

Page 11
16) Write a R program to concatenate two given matrices of same column but
different rows.
PROGRAM:
x = matrix(1:12, ncol=3)
y = matrix(13:24, ncol=3)
print("Matrix-1")
print(x)
print("Matrix-2")
print(y)
result = dim(rbind(x,y))
print("After concatenating two given matrices:")
print(result)
output :
[1] "Matrix-1"
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[1] "Matrix-2"
[,1] [,2] [,3]
[1,] 13 17 21
[2,] 14 18 22
[3,] 15 19 23
[4,] 16 20 24
[1] "After concatenating two given matrices:"
[1] 8 3
17) Write a R program to find row and column index of maximum and minimum
value in a given matrix.
Solution :
m = matrix(c(1:16), nrow = 4, byrow = TRUE)
print("Original Matrix:")
print(m)
result = which(m == max(m), arr.ind=TRUE)
print("Row and column of maximum value of the said matrix:")
print(result)
result = which(m == min(m), arr.ind=TRUE)
print("Row and column of minimum value of the said matrix:")
print(result)
output :
[1] "Original Matrix:"
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[1] "Row and column of maximum value of the said matrix:"
row col
[1,] 4 4

Page 12
[1] "Row and column of minimum value of the said matrix:"
row col
[1,] 1 1

18) Write a R program to append value to a given empty vector.


Solution :
vector = c()
values = c(0,1,2,3,4,5,6,7,8,9)
for (i in 1:length(values))
vector[i] <- values[i]
print(vector)

output :
[1] 0 1 2 3 4 5 6 7 8 9

19) Write a R program to multiply two vectors of integers type and length 3.
Solution:
x = c(10, 20, 30)
y = c(20, 10, 40)
print("Original Vectors:")
print(x)
print(y)
print("Product of two Vectors:")
z=x/y
print(z)
output:
"Original Vectors:"
[1] 10 20 30
[1] 20 10 40
[1] "Product of two Vectors:"
[1] 0.50 2.00 0.75

20) Write a R program to find Sum, Mean and Product of a Vector, ignore element
like NA or NaN.
Solution:
x = c(10, NULL, 20, 30, NA)
print("Sum:")
#ignore NA and NaN values
print(sum(x, na.rm=TRUE))
print("Mean:")
print(mean(x, na.rm=TRUE))
print("Product:")
print(prod(x, na.rm=TRUE))

output:
"Sum:"
[1] 60
[1] "Mean:"

Page 13
[1] 20
[1] "Product:"
[1] 6000

21) Write a R program to list containing a vector, a matrix and a list and give
names to the elements in the list.
Solution:
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])
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

Page 14
$`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

22) Write a R program to create a list containing a vector, a matrix and a list and
give names to the elements in the list. Access the first and second element of the
list.
Solution :
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])

output:
[1] "List:"
[[1]]
[1] "Red" "Green" "Black"

[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

Page 15
[[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

23) Write a R program to create a list containing a vector, a matrix and a list and
remove the second element.
Solution :
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)
print("Remove the second element of the list:")
list_data[2] = NULL
Page 16
print("New list:")
print(list_data)
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] "Remove the second element of the list:"


[1] "New list:"
[[1]]
[1] "Red" "Green" "Black"

[[2]]
[[2]][[1]]
[1] "Python"

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

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

24) Write a R program to select second element of a given nested list.


Solution:
x = list(list(0,2), list(3,4), list(5,6))
print("Original nested list:")
print(x)
e = lapply(x, '[[', 2)
print("Second element of the nested list:")
print(e)

output:
[1] "Original nested list:"

Page 17
[[1]]
[[1]][[1]]
[1] 0

[[1]][[2]]
[1] 2

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

[[2]][[2]]
[1] 4

[[3]]
[[3]][[1]]
[1] 5

[[3]][[2]]
[1] 6

[1] "Second element of the nested list:"


[[1]]
[1] 2

[[2]]
[1] 4

[[3]]
[1] 6

25) Write a R program to merge two given lists into one list.
Solution:
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:
[1] "Original lists:"
[[1]]
[1] 1

Page 18
[[2]]
[1] 2

[[3]]
[1] 3

[[1]]
[1] "Red"

[[2]]
[1] "Green"

[[3]]
[1] "Black"

[1] "Merge the said lists:"


[1] "New merged list:"
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "Red"

[[5]]
[1] "Green"

[[6]]
[1] "Black"

26) Write a R program to create a list named s containing sequence of 15 capital


letters, starting from ‘E’.
Solution:
l = LETTERS[match("E", LETTERS):(match("E", LETTERS)+15)]
print("Content of the list:")
print("Sequence of 15 capital letters, starting from ‘E’-")
print(l)
output:
[1] "Content of the list:"
[1] "Sequence of 15 capital letters, starting from ‘E’-"
[1] "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"

Page 19
27) Write a R program to assign new names "a", "b" and "c" to the elements of a
given list.
Solution:
list1 = list(g1 = 1:10, g2 = "R Programming", g3 = "HTML")
print("Original list:")
print(list1)
names(list1) = c("one", "two", "three")
print("Assign new names 'one', 'two' and 'three' to the elements of the said list")
print(list1)
output :
[1] "Original list:"
$g1
[1] 1 2 3 4 5 6 7 8 9 10

$g2
[1] "R Programming"

$g3
[1] "HTML"

[1] "Assign new names 'one', 'two' and 'three' to the elements of the said list"
$one
[1] 1 2 3 4 5 6 7 8 9 10

$two
[1] "R Programming"

$three
[1] "HTML"

28) Write a R program to find the levels of factor of a given vector.


Solution:
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)))
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"

29) Write a R program to create an ordered factor from data consisting of the
names of months.
Solution:
mons_v = c("March","April","January","November","January",
"September","October","September","November","August","February",
"January","November","November","February","May","August","February",
Page 20
"July","December","August","August","September","November","September",
"February","April")
print("Original vector:")
print(mons_v)
f = factor(mons_v)
print("Ordered factors of the said vector:")
print(f)
print(table(f))
output:
"Original vector:"
[1] "March" "April" "January" "November" "January" "September"
[7] "October" "September" "November" "August" "February" "January"
[13] "November" "November" "February" "May" "August" "February"
[19] "July" "December" "August" "August" "September" "November"
[25] "September" "February" "April"
[1] "Ordered factors of the said vector:"
[1] March April January November January September October
[8] September November August February January November November
[15] February May August February July December August
[22] August September November September February April
11 Levels: April August December February January July March May ...
September
f
April August December February January July March May
2 4 1 4 3 1 1 1
November October September
5 1 4

30) Write a R program to concatenate two given factor in a single factor.


Solution :
f1 <- factor(sample(LETTERS, size=6, replace=TRUE))
f2 <- factor(sample(LETTERS, size=6, replace=TRUE))
print("Original factors:")
print(f1)
print(f2)
f = factor(c(levels(f1)[f1], levels(f2)[f2]))
print("After concatenate factor becomes:")
print(f)
output:

[1] "Original factors:"


[1] Q Y M J J H
Levels: H J M Q Y
[1] B J L S F Z
Levels: B F J L S Z
[1] "After concatenate factor becomes:"
[1] Q Y M J J H B J L S F Z
Levels: B F H J L M Q S Y Z

Page 21

You might also like