R - Programming Slip


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

No comments:

Powered by Blogger.