Professional Documents
Culture Documents
Lab 1
Lab 1
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
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
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