You are on page 1of 3

RLAB

1. Simple Operations
a) Enter the data {2,5,3,7,1,9,6}directly and store it in a variable x.
b) Find the number of elements in x, i.e. in the data list.
c) Find the last element of x.
d) Find the minimum element of x.
e) Find the maximum element of x.
ANS:
a.x=c(2,5,3,7,1,9,6)
x
[1] 2 5 3 7 1 9 6

b. length(x)
[1] 7

c. x[length(x)]
[1] 6

d.> min(x)
[1] 2

e.> max(x)
[1] 9

2. Enter the data {1, 2, ... ,19,20} in a variable x.


a) Find the 3rd element in the data list.
b) Find 3rd to 5th element in the datalist.
c) Find 2nd, 5th, 6th, and 12th element in the list.
d) Print the data as {20, 19, …, 2, 1} without again entering the data
ANS:
a. x=c(1:20)
>x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

b. x[3]
[1] 3

c. x=c(3:5)
x
[1] 3 4 5

d.rev(x)
[1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

3. a) Create a data list (4, 4, 4, 4, 3, 3, 3, 5, 5, 5) using ‘rep’ function.


b) Create a list (4, 6, 3, 4, 6, 3, …, 4, 6, 3) where there 10 occurrences of 4, 6, and 3 in the
given order.
c) Create a list (3, 1, 5, 3, 2, 3, 4, 5, 7,7, 7, 7, 7,7, 6, 5, 4, 3, 2, 1, 34, 21, 54) using one line
command.
d) First create a list (2, 1, 3, 4). Then append this list at the end with another list (5, 7, 12,
6, -8). Check whether the number of elements in the augmented list is 11.
ANS:
a. rep(4,3,5)
[1] 4 4 4 4 4

b. rep(c(4,6,3),c(10))
[1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3

c. x=c(3,1,5,3,(2:5),rep(7,6),(6:1),34,21,54)
x
[1] 3 1 5 3 2 3 4 5 7 7 7 7 7 7 6 5 4 3 2 1 34 21 54

d. x<-c(2,1,3,4)
y<-c(5,7,12,6,-8)
append(x,y)
[1] 2 1 3 4 5 7 12 6 -8
length(append(x,y))
[1] 9
The length of the augmented list is 9.

4.
(a) Print all numbers starting with 3 and ending with 7 with an increment of 0:0.5. Store
these numbers in x.
(b) Print all even numbers between 2 and 14 (both inclusive)
(c) Type 2*x and see what you get. Each element of x is multiplied by 2.
ANS:
a. x<-seq(3,7,0.5)
>x
[1] 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0
b. seq(2,14,by=2)
[1] 2 4 6 8 10 12 14

c. 2*x
[1] 6 7 8 9 10 11 12 13 14

You might also like