0% found this document useful (0 votes)
36 views10 pages

R Slips

Uploaded by

Sameer Khan
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)
36 views10 pages

R Slips

Uploaded by

Sameer Khan
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/ 10

Q1. Write an R program to find the maximum and the minimum value of a given vector.

Ans:

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

Q.2 Write an R program to sort a Vector in ascending and descending order.

Ans:

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

Q3. Write an R program to compare two data frames to find the elements in first data frame
that are not present in second data frame.

Ans:-

Df_1 = data.frame( “item” = c(“item1”, “item2”, “item3”), “A” = c(1, 4, 12), “B” = c(2, 5, 15),
“C” = c(3, 6, 15)

Df_2 = data.frame( “item” = c(“item1”, “item2”, “item3”), “A” = c(1, 4, 12), “B” = c(2, 5, 15),
“C” = c(3, 7, 18)

)
Print(“Original Dataframes:”)

Print(df_1)

Print(df_2)

Print(“Row(s) in first data frame that are not present in second data frame:”)

Print(setdiff(df_1,df_2))

Q.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 22 nd to 24th letters in upper case.

Ans:

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

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

Ans:-

> x = c(10, 20, 30)

> print(“Sum:”)

> print(sum(x))

> print(“Mean:”)
> print(mean(x))

> print(“Product:”)

> print(prod(x))

Q.6. Write an R program to create a simple bar plot of five subject’s marks

Ans:

> marks = c(70, 95, 80, 74)

> barplot(marks,

+ main = “Comparing marks of 5 subjects”,

+ xlab = “Marks”,

+ ylab = “Subject”,

+ names.arg = c(“C++”, “Javascript”, “PHP”, “Rscript.”),

+ col = “darkred”,

+ horiz = FALSE)

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

Ans:-

Employees =

Data.frame(Name=c(“Archana”,”Doly”,”Raja”, “zinat”,”Lila”),

Gender=c(“M”,”M”,”F”,”F”,”M”), Age=c(23,65,34,24,45),

Designation=c(“Clerk”,”Manager”,”HR”,”CEO”,”A

SSISTANT”), mobile=c(“9876567865”,”8889765454”,”9879
065434”,”612398987”,”88867977576”))

Print(“Details of the employees:”)

Print(Employees)
Q.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.

Ans:-

X = c(10,20,10,10,40,50,20,30)

Y = c(10,30,10,20,0,50,30,30)

Print(“Original data frame:”)

C = data.frame(x,y)

Print©

Print(“Duplicate elements of the said data frame:”)

Print(duplicated©)

Print(“Unique rows of the said data frame:”)

Print(unique©)

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

Ans:-

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

Q10 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

Ans

Print(“ 1. Give names to the elements in the list.”)

List_data <- list(c(“A”, “B”,”C”), list(“Alibag”, “Mumbai”, “Delhi”))

Print(list_data)

Names(list_data) = c(“Alphabets”, “Cities(s)”)

Print(“List with column names:”)

Print(list_data)

Print(“2. Add an element at the end of the list.”)

List_data <- list(c(“A”, “B”,”C”), list(“Alibag”, “Mumbai”, “Delhi”))

Print(“List:”)

Print(list_data)

Print(“Add a new element at the end of the list:”)

List_data[4] = “Pune”

Print(“New list:”)

Print(list_data)

Print(“3. Remove the last element.”)

List_data <- list(c(“A”, “B”,”C”), list(“Alibag”, “Mumbai”, “Delhi”))

Print(list_data)

Print(“Remove the Last element of the list:”)

List_data[2] = NULL

Print(“New list:”)

Print(list_data)

Print(“4. Update the 3rd Element”)


List_data <- list(c(“A”, “B”,”C”), list(“Alibag”), list(“Neha”))

Print(list_data)

Print(“Update the second element of the list:”)

List_data[3] = “Big Data”

Print(“New list:”)

Print(list_data)

Q.11 Write a script in R to create two vectors of different lengths and give these vectors as
input to array and print addition and subtraction of those matrices.

Ans.

> v1<-c(10,20,30,40)

> v2<-c(2,3,4)

> matrix1=array(c(v1,v2),dim=(c(2,2,2)))

> print(matrix1)

> matrix2=array(c(v2,v1), dim=(c(2,2,2)))

> print(matrix2)

> matrix1+matrix2

> matrix1-matrix2

Q.12 Write an R Program to calculate Multiplication Table

Ans:-

Number <- as.integer(readline(“Enter the number: “))

Cat(“Multiplication Table for”, number, “:\n”) for (I in 1:10) {

Result <- number * i

Cat(number, “x”, I, “=”, result, “\n”)

}
Q13Consider the inbuilt iris dataset

i) Create a variable “y” and attach to it the output attribute of the “iris” dataset.
ii) Create a barplot to breakdown your output attribute.
iii) Create a density plot matrix for each attribute by class value.

Ans

Data(iris)

Dataset<-iris

Library()

X<-dataset[,1:4]

Y<-dataset[ ,5]

Library(dplyr)

Library(ggplot2)

Dataset %>% ggplot(aes(x=y)) + geom_bar() + labs(x = “Iris Flower Species”)

Scales<-list(x = list(relation = “free”), y = list(relation = “free”))

featurePlot(x = x, y = y, plot = “density”, scales = scales)

Slip14 Write an R program to concatenate two given factor in a single factor and display in
descending order.

Ans

Factor1 <- factor(c(“Apple”, “Banana”, “Cherry”, “Date”))

Factor2 <- factor(c(“Fig”, “Grape”, “Honeydew”))

Combined_factor <- c(factor1, factor2)

Combined_factor <- factor(combined_factor, levels = rev(unique(combined_factor)))

Print(combined_factor)

Slip15
Write an R program to extract the five of the levels of factor created from a random sample
from the LETTERS

Ans

Sample_letters <- sample (LETTERS, 20, replace = TRUE)

Sample_factor <- factor(sample_letters)

First_five_levels <- levels(sample_factor) [1:5]

Print(first_five_levels)

Q16 Consider the inbuilt mtcar dataset

i) Subset the vector, “mtcars[,1]”, for values greater than “15.0”.


ii) Subset “airquality” for “Ozone” greater than “28”, or “Temp” greater than “70”.

Return the first five rows.

iii) Subset “airquality” for “Ozone” greater than “100”. Select the columns
“Ozone”,”Temp”, “Month” and “Day” only.

Ans

Data(mtcars)

Data(airquality)

Subset_mtcars <- mtcars[mtcars[, 1] > 15.0,]

Subset_airquality_2 <-head(airquality[airquality$Ozone > 28 | airquality$Temp > 70, ], 5)

Subset_airquality_3 <-airquality[airquality$Ozone > 100, c(“Ozone”, “Temp”, “Month”,


“Day”)]

Slip17 Write an R Program to calculate Decimal into binary of a given number.

➢ Binary_conversion<-function(n){

+ if(n>1){

+ binary_conversion(as.integer(n/2))

+}
+ cat(n%%2

+}

➢ Binary_conversion(11)

Q18 Write an R program to create three vectors a,b,c with 3 integers. Combine the three
vectors to become a 3x3 matrix where each column represents a vector. Print the content
of the matrix.

Ans

A <- c(1, 2, 3)

B <- c(4, 5, 6)

C <- c(7, 8, 9)

Matrix_result <- matrix(data = c(a, b, c), nrow = 3, byrow = FALSE)

Print(matrix_result)

Q19 – Write an R program to draw an empty plot and an empty plot specify the axes limits
of the graphic.

Solution:

Plot.new()

Plot(1, type=”n”, xlab=””, ylab=””, xlim=c(0, 20), ylim=c(0, 20))

Q20 – Consider Weather dataset i) Selecting using the column number ii) Selecting using
the column name iii) Make a scatter plot to compare Wind speed and temperature

Ans

Library(devtools)

Install_github(“Ram-N/weatherData”)

Dim(London2013)

Colnames(London2013)
London2013%>% select(1:2)

London2013%>% select(Time)

You might also like