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

1.R Programs

Uploaded by

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

1.R Programs

Uploaded by

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

1.

R program to find the maximum and the minimum value of a given vector

nums = c(10, 20, 30, 40, 50, 60)

print('Original vector:')

print(nums)

print(paste("Maximum value of the said vector:",max(nums)))

print(paste("Minimum value of the said vector:",min(nums)))

2.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))

3.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))

4.Write an R program to extract first 10 English letter in lower case and last 10 letters in upper
case and extract letters between 22nd to 24th letters in uppercase.

print("First 10 letters in lower case:")

t = head(letters, 10)

print(t)

print("Last 10 letters in upper case:")

t = tail(LETTERS, 10)

print(t)

print("Letters between 22nd to 24th letters in upper case:")

e = tail(LETTERS[22:24])

print(e)

5.Write an R program to find Sum, Mean and Product of a Vector.

x = c(10, 20, 30)

print("Sum:")

print(sum(x))

print("Mean:")

print(mean(x))

print("Product:")

print(prod(x))
6.Write an R program to create a simple bar plot of five subject’s marks.

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)

7.Write an R program to create a Dataframes which contain details of 5 employees and display the
details in ascending order.

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","ASSISTANT"),

SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","679-77-576")

print("Details of the employees:")

print(Employees)

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

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

9.Write an R program to change the first level of a factor with another level of a given factor.

v = c("a", "b", "a", "c", "b")

print("Original vector:")

print(v)

f = factor(v)

print("Factor of the said vector:")

print(f)

levels(f)[1] = "e"

print(f)

(Imp-Factors are the data objects which are used to categorize the data and store it as levels. They can
store both strings and integers. They are useful in the columns which have a limited number of unique
values. Like "Male, "Female" and True, False etc. They are useful in data analysis for statistical
modeling.

Factors are created using the factor () function by taking a vector as input.

levels 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.)

10.Write a script in R to create a list of cities and perform the following 1) Give names to the elements in
the list. 2) Add an element at the end of the list. 3) Remove the last element. 4) Update the 3rd Element

You might also like