You are on page 1of 4

R Vectors

Generating sequences

How to display first 30 natural numbers using (:) operator? Ans


What difference is there between commands > 1:(25-1) and >1:25-1? Ans
How to display sequence 5, 10, 15, ......................... , 50 using seq() function? Ans.
How to print sequence 100, 90, 80 , ..............., 0 using seq() function? Ans.
How to display 9 values between 1 and 5 using seq() function? Ans.
How to use 'c()' function to convert a set of values as a vector? Ans.
How to read data input from the keyboard as elements of a vector? Ans.
How to create a vector containing same value repeated specified number of times? Ans.
How to create two sequences of natural numbers? Ans.
How to create multiple sequences of natural numbers of specified size? Ans.
How to create a vector containing strings such as names of some fruits? Ans.
Given vector t<-c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"). Using position indexing how can
we access element "Mon" ? Ans.
Given vector t<-c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"). Using position indexing how can
we access elements "Mon" and "Fri"? Ans.
Given vector t<-c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"). Using logical indexing how can we
access elements "Mon" and "Fri"? Ans.
Given vector t<-c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"). Using negative indexing how can
we access elements "Mon", "Tue" and "Fri"? Ans.
How to add/ subtract/ multiply/ divide contents of two numeric vectors of same length given as
v1<-c(4,7,12,3,6,2) and v2<-c(4,6,4,3,2,5)? Ans.
While applying arithmetic operations on two vectors, what happens if two operand vectors are not
of same length? Ans.
How to sort elements of a vector in ascending order? Ans.
How to sort elements of a vector in descending order? Ans.
R Lists

What are lists? Ans.


What command is used to create a list? Ans.
How to create a list of name aa containing name of a student, say, "Rabi", his marks in any three
subjects, say, 35, 65,25, his status of fee payment, say as, TRUE, and his GPA in last exam, say, 3.2?
Ans.
In above example, how to treat marks in three subjects as a single vector while creating list? Ans.
How to create a list of name 'bb' containing a vector of names of first three month, i.e., Jan, Feb,
Mar; a matrix of any 6 numbers in 2 rows, and a list containing elements "Rabi" and 12.5? Ans.
How to give names "Months", "MatA" and "Datum" to list 'bb' created above? Ans.
How to access the elements in above three named objects? Ans.
How to access element 'Feb' in vector named 'Months' in list named 'bb'? Ans.
How to access element in 2nd row and 2nd column of matrix named 'MatA'? Ans.
How to change the value of element in 2 nd row and 2nd column of 'MatA' in list 'bb' to 9? Ans.
How to change the add new elements to an existing list? (Later)
R Matrices
What difference is there between R-vector and R-matrix? Ans.
Which command is used to create a matrix? Ans.
Write the syntax of 'matrix()' command. Ans.
How to create a matrix of three columns, first containing elements 7 and 12, second containing
elements 3 and 2 and third containing elements 3 and 5. Ans.
how can above matrix be created by using 'nrow' argument? Ans.
How to create a matrix of two rows, first containing elements 5, 3, 1 and second containing elements
4, 3, 2? Ans.
How to create a matrix of 12 integers from 3 to 14 arranged squenctially in 3 columns as?

Ans.
How to create a matrix of 12 integers from 3 to 14 arranged sequentially in 3 rows as-

? Ans.
In above matrix how to provide titles 'Row1', 'Row2', 'Row3' to the rows and 'Col1', 'Col2', 'Col3' and
'Col4' to the columns? Ans.
How to access and display the element in 2 nd row and third column of above matrix? Ans.
How to display all elements of second row? Ans.
How to display all elements of second column? Ans.
Suppose there are two matrices of name 'mm' and 'nn'. How to (i) add (ii) subtract (iii) multiply and
(iv) divide them?
R-Arrays

What are arrays? Ans.


How to create two matrices containing following elements by using arrays?

Ans.
vec1<-c(2,5,6)
vec2<-c(4,6,7)
vec3<-c(3,2,5)
arr<-array(c(vec1,vec2,vec3), dim = c(3,3,2))
arr
Or,
arr<-array(c(2,5,6,4,6,7,3,2,5), dim=c(3,3,2))

How to give names "Row1", "Row2", "Row3", "Col1", "Col2", "Col3" to row names and column
names as well as names "Matrix1" and "Matrix2" to above matrices?
rnam<-c("Row1", "Row2", "Row3")
cnam<-c("Col1", "Col2", "Col3")
matnam<-c("Matrix1","Matrix2")
arr<-array(c(2,5,6,4,6,7,3,2,5), dim=c(3,3,2), dimnames=list(rnam, cnam, matnam))
arr
How to print the element in the first row and third column of the second matrix? Ans.
How to display all elements in the third row of the first matrix? Ans.
How to display all elements in the second column of the first matrix? Ans.
How to display all elements of the second matrix? Ans.
How to extract two matrices in 'arr1' in names 'mm1' and 'mm2'? Ans.
How to display sum of elements row-wise of both matrices? Ans.
How to display sum of elements column-wise of both matrices? Ans.
How to display sum of elements matrix-wise of both matrices? Ans.
How to display mean of elements row-wise of both matrices? Ans.

You might also like