Results for R Programming Slip

R - Programming Slip

December 28, 2020

Write a script in R to create a list of cities and perform the following

Q. Write a script in R to create a list of cities and perform the following.

Answer :

1) Give names to the elements in the list.
list_data <- ava="" br="" c="" ed="" lack="" list="" reen="" ython=""> print(list_data)
names(list_data) = c("Color", "Language(s)")
print("List with column names:")
print(list_data)
2) Add an element at the end of the list.
list_data <- ava="" br="" c="" ed="" lack="" list="" reen="" ython=""> print("List:")
print(list_data)
print("Add a new element at the end of the list:")
list_data[4] = "New element"
print("New list:")
print(list_data)
3) Remove the last element.
list_data <- ava="" br="" c="" ed="" lack="" list="" reen="" ython=""> print(list_data)
print("Remove the Last element of the list:")
list_data[2] = NULL
print("New list:")
print(list_data)
4) Update the 3rd Element
list_data <- br="" c="" ed="" lack="" list="" reen="" saurabh="" ython=""> print(list_data)
print("Update the second element of the list:")
list_data[3] = "R programming"
print("New list:")
print(list_data)

Q. 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.

Answer : 

m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
print("Matrix-1:")
print(m1)
m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
print("Matrix-2:")
print(m2)

result = m1 + m2
print("Result of addition")
print(result)

result = m1 - m2
print("Result of subtraction")
print(result)


Q. Write an R Program to calculate Multiplication Table.

Answer: 

num = as.integer(readline(prompt = "Enter a number: "))
for(i in 1:10) {
print(paste(num,'x', i, '=', num*i))
}

R - Programming Slip R - Programming Slip Reviewed by technical_saurabh on December 28, 2020 Rating: 5

R Programming Slip

December 28, 2020
Write an R program to create a Data frames which contain details of 5 employees  and display the details in ascending order.

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

Answer :

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)


Q. 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.

Answer:

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


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

Answer:

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)

R Programming Slip R Programming Slip Reviewed by technical_saurabh on December 28, 2020 Rating: 5

R Programming Slip

December 22, 2020

 


Slip 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 upper case. 

Answer : 

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)

slip 5

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

Answer : 

x = c(10, 20, 30)
print("Sum:")
print(sum(x))
print("Mean:")
print(mean(x))
print("Product:")
print(prod(x))

Slip 6

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

Answer : 

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)

R Programming Slip R Programming Slip Reviewed by technical_saurabh on December 22, 2020 Rating: 5

R Programming Slip

December 22, 2020

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


 Slip No 1. 

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

Answer: 

num = c(10, 20, 30, 40, 50, 60)
print(num)
print(paste("Maximum value of a given vector :",max(num)))
print(paste("Minimum value of a given vector :",min(num)))

R Programming Slip R Programming Slip Reviewed by technical_saurabh on December 22, 2020 Rating: 5
Powered by Blogger.