Big Data R Programming Solved Slips
#Write an R program to find the maximum and the minimum value of a given vector.
x = c(50, 10, 33, 12, 19, 36)
print("Original vector: ")
print(x)
print("Maximum value of the above Vector:")
print(max(x))
print("Minimum value of the above Vector:")
print(min(x))
#Write an R program to sort a Vector in ascending and descending order.
x = c(50, 10, 33, 12, 19, 36)
print("Original Vector: ")
print(x)
print("Sort in ascending order: ")
print(sort(x))
print("Sort in descending order:")
print(sort(x, decreasing=TRUE))
#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.
a = c("a", "b", "c", "d", "e")
b = c("d", "e", "f", "g")
print("Original Dataframes")
print(a)
print(b)
print("Data in first dataframe that are not present in second dataframe:")
result = setdiff(a, b)
print(result)
#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 upper case.
print("First 10 letters in lower case:")
first10 = head(letters, 10)
print(first10)
print("Last 10 letters in upper case:")
last10 = tail(LETTERS, 10)
print(last10)
print("Letters between 22nd to 24th letters in upper case:")
inBetween = tail(LETTERS[22:24])
print(inBetween)
#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))
#Write an R program to create a simple bar plot of five subject's marks
marks = c(80,85,81,90,87)
barplot(marks,
main = "Compairing Marks of 5 subject",
xlab = "subject",
ylab = "marks",
names.arg = c("DM","DS","Soft. Engg.","A JS","Big Data"),
col = "darkblue",
horiz = FALSE)
#Write an R program to create a Dataframe which contain details of 5 employees and display the details in ascending order.
Employees = data.frame(
Name=c("Anant", "Divas", "Kartiki", "Jivan", "Leela"),
Gender=c("M","M","F","M","F"),
Age=c(23,22,25,26,32),
Designation=c("Clerk", "Manager", "Executive", "CEO", "ASSISTANT"),
SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","679-77-576")
)
print(Employees[with(Employees, order(Age)),])
#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))
#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.
# Create two vectors of different lengths.
vector1 <- c(7,4,9)
vector2 <- c(10,11,12,13,14,15)
# Take these vectors as input to the array.
array1 <- array(c(vector1,vector2),dim = c(3,3,1))
# Create two vectors of different lengths.
vector3 <- c(2,3,6)
vector4 <- c(6,0,11,3,14,1,2,6,9)
# Take these vectors as input to the array.
array2 <- array(c(vector3,vector4),dim = c(3,3,1))
# create matrices from these arrays.
matrix1 <- array1[,,1]
matrix2 <- array2[,,1]
# Addition
result1 <- matrix1+matrix2
print(result1)
# Subtraction
result2 <- matrix1-matrix2
print(result2)
#Write an R Program to calculate Multiplication Table
num = as.integer(readline(prompt = "Enter a number: "))
for(i in 1:10)
{
print(paste(num, 'x', i, '=', num*i))
}
# paste: paste function is used to concatenate text and variables separated by commas
#Consider 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.
install.packages("caret")
library(caret)
data(iris)
dataset<-iris
x <- dataset[,1:4]
y <- dataset[,5]
plot(y)
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="density", scales=scales)
#Write an R program to concatenate two given factor in a single factor and display in descending order.
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)
print("Conatenate in descending order:")
print(rev(f))
#Consider the inbuilt mtcar dataset
#i) Subset the vector, "mtcars[,1]", for values greater than "15.0".
subset(mtcars[,1], mtcars[,1] > 15.0)
#ii) Subset "airquality" for "Ozone" greater than "28", or "Temp" greater than "70". Return the first five rows.
head(subset(airquality, Ozone > 28 | Temp > 70))
#iii) Subset "airquality" for "Ozone" greater than "100". Select the columns "Ozone", "Temp", "Month" and "Day" only.
subset(airquality, Ozone > 100, select = c(Ozone, Temp, Month, Day))
#Write an R Program to calculate Decimal into binary of a given number.
convert_to_binary <- function(n) {
if(n > 1) {
convert_to_binary(as.integer(n/2))
}
cat(n %% 2)
}
decimal = 10.23
print("Decimal number")
print(decimal)
print("Binary number")
binary = convert_to_binary(decimal)
#Write an 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.
a<-c(1, 2, 3)
b<-c(4, 5, 6)
c<-c(7, 8, 9)
# Combine vectors to form a matrix
m<-cbind(a, b, c)
print("Content of the said matrix:")
print(m)
#Write an R program to draw an empty plot and an empty plot specify the axes limits of the graphic.
plot.new()
plot(1,type = "n",xlab = "", ylab = "",xlim = c(0,20),ylim = c(0,20))
#Consider the plantGrowth inbuilt dataset
#i) Create a variable "y" and attach to it the output attribute of the "plantGrowth" dataset.
data("PlantGrowth")
dataset<-PlantGrowth
#ii) Create a barplot to breakdown your output attribute.
x <- dataset[,1:2]
y <- dataset[,30]
barplot(y)
#iii) Create a density plot matrix for each attribute by class value
ggplot(PlantGrowth, aes(PlantGrowth$weight)) + geom_density(aes(data = PlantGrowth$weight, fill = PlantGrowth$group), position = 'identity', alpha = 0.5) + labs(x = 'Weight', y = 'Density') + scale_fill_discrete(name = 'Group') + scale_x_continuous(limits = c(2, 8))
#Write an R program to print the numbers from 1 to 100 and print "SY" for multiples of 3, print "BBA" for multiples of 5, and print "SYBBA" for multiples of both.
for(i in 1:100) {
if(i %% 3 == 0 & i %% 5 == 0) {
print("SYBBA")
}
else if(i %% 3 == 0) {
print("SY")
}
else if (i %% 5 == 0) {
print("BBA")
}
else {
print(i)
}
}
#Write an R program to create a list of elements using vectors, matrices and a functions. Print the content of the list.
l = list(
c(1, 2, 2, 5, 7, 12),
month.abb,
matrix(c(3, -8, 1, -3), nrow = 2),
asin
)
print(l)
#Write an R program to convert a given matrix to a list and print list in ascending order.
m = matrix(1:10,nrow=2, ncol=2)
print("Original matrix:")
print(m)
l = split(m, rep(1:ncol(m), each = nrow(m)))
print("list from the said matrix:")
print(l)
#Write an R program to sort a list of 10 strings in ascending and descending order
simple_list <- c("abc", "pqr", "xyz", "aaa", "lmn", "www", "efg", "klk", "plm", "uvw")
# Ascending
simple_list <- sort(simple_list, decreasing = FALSE)
print(simple_list)
# Descending
simple_list <- sort(simple_list, decreasing = TRUE)
print(simple_list)
0 Comments