R Slips
R Slips
Ans:
> print(nums)
Ans:
> print(x)
> print(sort(x))
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(t)
> print(t)
> e = tail(LETTERS[22:24])
> print€
Ans:-
> 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:
> barplot(marks,
+ xlab = “Marks”,
+ ylab = “Subject”,
+ 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(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)
C = data.frame(x,y)
Print©
Print(duplicated©)
Print(unique©)
Q.9 Write an R program to change the first level of a factor with another level of a given
factor.
Ans:-
> print(v)
> f = factor(v)
> print(f)
> print(f)
Q10 Write a script in R to create a list of cities and perform the following
Ans
Print(list_data)
Print(list_data)
Print(“List:”)
Print(list_data)
List_data[4] = “Pune”
Print(“New list:”)
Print(list_data)
Print(list_data)
List_data[2] = NULL
Print(“New list:”)
Print(list_data)
Print(list_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)
> print(matrix2)
> matrix1+matrix2
> matrix1-matrix2
Ans:-
}
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)
Slip14 Write an R program to concatenate two given factor in a single factor and display in
descending order.
Ans
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
Print(first_five_levels)
iii) Subset “airquality” for “Ozone” greater than “100”. Select the columns
“Ozone”,”Temp”, “Month” and “Day” only.
Ans
Data(mtcars)
Data(airquality)
➢ 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)
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()
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)