Teja Swaroop R 1-30
Teja Swaroop R 1-30
R-PROGRAMMING
[Document subtitle]
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-1
AIM: To implement a R program to take input from the user (name and
age) and display the values.
DESCRIPTION:
Using read line () method
Using scan () method
Using read line () method
In R language read line () method takes input in string format. If one inputs an
integer then it is inputted as a string, let say, one wants to input 255, then it will
input as “255”, like a string.
Source code:
name = read line (prompt="Input your name: ")
print (paste ("My name is ", name, "and I am", age, "years old."))
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE -2
AIM: To implement a R program to get the details of the objects in memory.
DESCRIPTION:
Lists are the R objects which contain elements of different types
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
like numbers, strings, vectors and another list inside it. A list can also
contain a matrix or a function as its elements. List is created using list
() function.
Source code:
name = "Python";
n1 = 10;
n2 = 0.5
num s = c (10, 20, 30, 40, 50, 60)
print (Ls ())
print ("Details of the objects in memory:")
print (ls .str ())
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-3
AIM: To implement 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.
DESCRIPTION:
S e q () function in R Language is used to create a sequence of elements in
a Vector. It takes the length and difference between values as optional
argument. Syntax: s e q (from, to, by, length. out)04
SOURCE CODE:
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:")
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
print (sum (51:91))
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-4
AIM: To implement a R program to create a simple bar plot of five subject
marks.
DESCRIPTION:
bar plot ()
A bar chart represents data in rectangular bars with length of the bar
proportional to the value of the variable. R uses the function bar plot () to
create bar charts. R can draw both vertical and Horizontal bars in the bar chart.
In bar chart each of the bars can be given different colours.
SOURCE CODE:
marks = c (70, 95, 80, 74)
bar plot (marks,
main = "Comparing marks of 5 subjects",
x lab = "Marks",
y lab = "Subject",
names= c
("English",
"Science",
"Math.",
"Hist."),
col = "dark
red ",
horizontal
= FALSE)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-5
Aim: To write a R program to get the unique elements of a given string and
unique numbers of vector.
DESCRIPTION:
str ()
Strings are basically a bunch of character variables. It is a one-dimensional array of
characters. One or more characters enclosed in a pair of matching single or double
quotes can be considered as a string in R. Strings represent textual content and can
contain numbers, spaces and special characters. An empty string is represented by
using “”. Strings are always stored as double-quoted values in R.
SOURCE CODE:
str1 = "The quick brown fox jumps over the lazy dog."
Print ("Original vector(string)")
print(str1)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
print ("Unique elements of the said vector:")
print (unique (to lower(str1)))
num s= c (1, 2, 2, 3, 4, 4, 5, 6)
print ("Original vector(number)")
print (num s)
print ("Unique elements of the said vector:")
print (unique (num s))
EXERCISE-6
AIM: To 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
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
of the matrix.
DESCRIPTION:
Vectors : -
A vector is simply a list of items that are of the same type.To combine the list of
items to a vector, use the c () function and separate the items by a comma.
SOURCE CODE:
a<-c (1,2,3)
b<-c (4,5,6)
c<-c (7,8,9)
m<-c bind (a, b, c)
print ("Content of the said matrix:")
print(m)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE -7
AIM: To write a R program to create a 5 x 4 matrix 3 x 3 matrix with labels and fill the
matrix rows and 2 × 2 matrix with labels and fill the matrix by columns.
DESCRIPTION:
Rows and columns:
rows and columns from a given data frame. When working on data
analytics or data science projects these commands come in handy in data
cleaning activities.
SOURCE CODE:
m1 = matrix (1:20, n row=5, n col=4)
print ("5 × 4 matrix:")
print(m1)
cells = c (1,3,5,7,8,9,11,12,14)
r names = c ("Row1", "Row2", "Row3")
c names = c ("Col1", "Col2", "Col3")
m2 = matrix (cells, n row=3, n col=3, by row=TRUE, dim names=list (r names,
c names))
print ("3 × 3 matrix with labels, filled by rows: ")
print(m2)
print ("3 × 3 matrix with labels, filled by columns: ")
m3 = matrix (cells, n row=3, n col=3, by row=FALSE, dim names=list (r
names, c names))
print(m3)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE -8
Aim: to 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.
DESCRIPTION:
C bind ():
Function in R Language is used to combine specified Vector, Matrix or Data Frame by
columns
R bind ():
The r bind function in R, short for row-bind, can be used to combine vectors,
matrices and data frames by rows.
SOURCE CODE:
num1 = r bind(rep("A",3), rep("B",3), rep("C",3))
print("num1")
print(num1)
num2 = r bind (rep("P",3), rep("Q",3), rep("R",3))
print("num2")
print(num2)
num3 = r bind (rep("X",3), rep("Y",3), rep("Z",3))
print("num3")
print(num3)
a = matrix (t (c bind (num 1, num2, num3)), n col=3, by row=T)
print ("Combine three arrays, taking one row from each one by one:")
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
print(a)
EXERCISE-9
Aim: Write a R program to create a two-dimensional 5x3 array of sequence of
even integers greater than 50.
DESCRIPTION:
i) Length out: desired length of the sequence.
ii) From: is used for starting value or starting point of sequence.
SOURCE CODE:
a <- array (seq (from = 50, length. out = 15, by = 2), c (5, 3))
print ("Content of the array:")
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
print ("5×3 array of sequence of even integers greater than 50:")
print(a)
EXERCISE -10
AIM: 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.
DESCRIPTION:
Data frame: Data Frames are data displayed in a format as a table. Data Frames
can have different types of data inside it. While the first column can be
character, the second and third can be numeric or logical.
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
array1 = array(1:30, dim=c(3,5,2))
print(array1)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-11
AIM: To implement a R program to create an empty data frame
DESCRIPTION:
Data. Frame: Data Frames are data displayed in a format as a table. data
Frames can have different types of data inside it. While the first column can be
character, the second and third can be numeric or logical.
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-12
AIM:TO Write a R program to concatenate two given matrices of same
column but different rows.
DESCRIPTION:
Duplicated (): that determines which elements of a vector or data frame are
duplicates of elements with
smaller subscripts and returns a logical vector indicating which elements (rows)
are duplicates.
Unique: used to eliminate or delete the duplicate values or the rows present in
the vector, data frame, or matrix as well.
SOURCE CODE:
name = c ('Sia', 'sai', 'kiran ',’raj’, 'Emma')
score = c (12.5, 9, 16.5, 12,)
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)
d f = data. Frame (name, score, attempts, qualify)
print (d f)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-13
AIM: TO 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.
DESCRIPTION:
Duplicated (): determines which elements of a vector or data frame are
duplicates of elements with smaller subscripts, and returns a logical vector
indicating which elements (rows) are duplicates.
Unique (); used to eliminate or delete the duplicate values or the rows present
in the vector, data frame, or matrix as well.
SOURCE CODE:
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))
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-14
AIM: TO 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
DESCRIPTION:
Save (): function writes an external representation of R objects to the specified file. The
objects can be read back from the file at a later date by using the function load
load (): all of the R objects saved in the file are loaded into R. The names given to these
objects when they were originally saved will be given to them when they are loaded.
SOURCE CODE:
exam_data = data.frame(
name =
c(‘antasia’,’dina’,’katherine’,’james’,michaeal’,’emily’,’laura’,’mathew’)
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")
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-15
AIM: to write a R program to create a matrix from a list of given vectors.
DESCRIPION:
FOR LOOP: type of control statement that enables one to easily construct a
loop that has to run statements or a set of statements multiple times.
SOURCE CODE:
l = list ()
for (I in 1:5) l [[ I]] <- c (I, 1:4)
print ("List of vectors:")
print(l)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
result = do. Call (r bind, l)
print ("New Matrix:")
print(result)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-16
AIM: Write a R program to concatenate two given matrices of same column
but different rows.
DESCRIPTION:
Dim (): is an inbuilt R function that either sets or returns the dimension of the matrix, array,
or data frame. The dim () function takes the R object as an argument and returns its
dimension, or if you assign the value to the dim () function, then it sets the dimension for that
R Object.
SOURCE CODE:
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)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE
-17
AIM: to implement
a R program to find
row and column
index of maximum
and minimum value
in a given matrix.
DESCRIPTION:
Which (): R returns
the position or the index of the value which satisfies the given condition.
Syntax:
SOURCE CODE:
m = matrix (c (1:16), n row = 4, by row = 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.in d =TRUE)
print ("Row and column of minimum value of the said matrix:")
print(result)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE : 18
AIM: Write a R program to append value to a given empty vector.
DESCRIPTION:
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
vector is a basic data structure which plays an important role in R
programming. In R, a sequence of elements which share the same data type is
known as vector.
Source code:
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)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
Exercise-19
AIM: Write a R program to multiply two vectors of integers type and length 3.
DESCRIPTION:
Int vector:
numeric value is known as integer data. This integer data is represented by &
quo t; Int.& quo t; The Int size is bytes and long Int size of 4 bytes. A vector
which contains integer elements is known as an integer vector.
SOURCE CODE:
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)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-20
AIM: Write a R program to find Sum, Mean and Product of a Vector, ignore
element like NA OR Na N.
DESCRIPTION:
R has two different ways of representing missing data and understanding each is important
for the user. Na N means “not a number” and it means there is a result, but it cannot be
represented in thecomputer.NA ex plain s that the data is just missing for unknown reasons.
SOURCE CODE:
x = c (10, NULL, 20, 30, NA)
print("Sum:")
Print (sum (x, na.rm=TRUE))
print("Mean:")
print (mean (x, na.rm=TRUE))
print("Product:")
print (prod (x, na.rm=TRUE))
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-21
AIM: Write a R program to list containing a vector, a matrix and a list and give
names to the elements in the list.
DESCRIPTION:
List (): A list in R can contain many different data types inside it. A list is a
collection of data which is ordered and changeable. To create a list, use the list()
function.
SOURCE CODE:
List data <- list (c (" Red", "Green", "Black"), matrix (c (1,3,5,7,9,11), n row =
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])
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-22
AIM: 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.
DESCRIPTION:
List (): A list in R can contain many different data types inside it. A list is a
collection of data which is ordered and change able To create a list, use the list
() function.
SOURCE CODE:
List data <- list (c (" Red", "Green", "Black"), matrix (c (1,3,5,7,9,11), row = 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])
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE -23
AIM: Write a R program to create a list containing a vector, a matrix and a list
and remove the second element.
DESCRIPTION:
remove an element from a list in R, we can remove items from a list
is with the position index. To remove elements, we set the value to
e g: l s t = list (1, 2, 3) l s t [[1]] = NULL # remove the first element.
SOURCE CODE:
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-24
AIM: Write a R program to select second element of a given nested list.
DESCRIPTION:
L apply () function in the R Language takes a list, vector, or data frame as input
and gives output in
the form of a list object.
Source code:
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:")
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
print(e)
EXERCISE-25
AIM: Write a R program to merge two given lists into one list.
DESCRIPTION:
we are using variables lst1, lst2 for holding the list elements of two different
types. Call the function for merging the two lists and assign them to a list
variable.
SOURCE CODE:
n1 = list (1,2,3)
c1 = list ("Red", "Green", "Black")
print ("Original lists:")
print(n1)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
print(c1)
print ("Merge the said lists:")
m list= c (n1, c1)
print ("New merged list:")
print (m
list)
EXERCISE-26
AIM: Write a R program to create a list named s containing sequence of 15
capital letters, starting from ‘E’.
DESCRIPTION:
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
To create a sequential uppercase alphabet in R, use the LETTERS constant. The
LETTERS is a character constant in R that generates an uppercase alphabet, and
you can use it with different functions to extract the result as per your
requirement.
SOURCE CODE:
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)
E
X
E
R
CI
SE
-
27
AI
M:
To implement a R program to assign new names "a", "b" and "c" to the elements
of a given list.
DESCRIPTI0N:
Names (): this function in R Language is used to get or set the name of an
Object. This function takes
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
object I. e. vector, matrix or data frame as argument along with the value that is
to be assigned as
name to the object. The length of the value vector passed must be exactly equal
to the length of the
object to be named.
SOURCE CODE:
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
)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-28
AIM: to implement a r program to find the levels of factor of a given vector.
DESCRIPTION:
Levels() function provides access to the levels attribute of a variable. The first form returns
the
value of the levels of its argument and the second sets the attribute.
Factor : in R is a variable used to categorize and store the data, having a limited number of
different
values. It stores the data as a vector of integer values. Factor in R is also known as a
categorical
variable that stores both string and integer data values as levels.
SOURCE CODE:
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)))
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
EXERCISE-29
AIM: To implement a r program to create an ordered factor from data
consisting of the names of months.
DESCRIPTION:
Table (): function in R Language is used to create a categorical representation of
data with variable name and the frequency in the form of a table.
SOURCE CODE:
mons_v = c("March","April","January","November","January",
"September","October","September","November","August","February",
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
"January","November","November","February","May","August","February",
"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(ta
ble(f))
EXERCISE-30
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
AIM: to implement a r program to concatenate two given factors in a single
factor.
DESCRIPTION:
takes a sample of the specified size from the elements of
replacement using either with or without
Size:
Provides the generic function and S4 methods to get the size of each element
from objects
based on  9; & g t; item Matrix. For example, it is used to get a vector of
transaction sizes (i.e., the
number of present items (ones) per element (row) of the binary incidence
matrix) from an object of
class ' & g t; transactions)
The replace () function in R can be used to replace specific elements in a vector
with new values.
SOURCE CODE:
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)
K.TEJA SWAROOP
21HQ1A0529
AVANTHI’S RESEARCH AND TECHNOLOGICAL ACADEMY
R-PROGRAMMING LAB
K.TEJA SWAROOP
21HQ1A0529