0% found this document useful (0 votes)
152 views

Exercise 1 Solution

This document contains an exercise with 9 questions about using vectors in R. The questions involve creating, manipulating, and extracting elements from vectors. Suggested solutions to each question are provided showing how to use functions like seq, rep, sort, and indexing to generate and work with vectors in R as required by each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Exercise 1 Solution

This document contains an exercise with 9 questions about using vectors in R. The questions involve creating, manipulating, and extracting elements from vectors. Suggested solutions to each question are provided showing how to use functions like seq, rep, sort, and indexing to generate and work with vectors in R as required by each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EIA1013

PROGRAMMING TOOLS FOR ECONOMICS

EXERCISE 1
VECTORS

QUESTIONS

1. Using R, verify that


6𝑎 + 42
= 29.50556
34.2−3.62
when a = 2.3.

2. Calculate the square root of half of the average of the numbers 25.2, 15, 16.44, 15.3
and 18.6 by using R.

3. Create an object that stores the value 32 × 41/8 . Overwrite your object by itself divided
by 2.33. Print the result to the console.

4. Create and store a sequence of values from 5 to −11 that progresses in steps of 0.3.
Overwrite the object using the same sequence with the order reversed.

5. Repeat the vector c(-1,3,-5,7,-9) twice, with each element repeated 10 times, and store
the result. Display the result sorted from largest to smallest.

6. Create and store a vector that contains the following, in this order:
– A sequence of length 5 from 3 to 6 (inclusive)
– A twofold repetition of the vector c(2,-5.1,-33)
7
– The value 42 + 2
Extract the first and last elements of your vector, storing them as a new object.

7. Create and store a vector that contains the following:


– A sequence of integers from 6 to 12 (inclusive)
– A threefold repetition of the value 5.3
– The number −3

8. Use the vector c(2,4,6) and the vector c(1,2) in conjunction with rep and * to produce
the vector c(2,4,6,4,8,12).

9. A sequence of nine values starting at 102 and ending at 100.


SUGGESTED SOLUTIONS

Q1
> a<-2.3
> (6*a+42)/(3^(4.2-3.62))
[1] 29.50556

Q2
> sqrt(x=0.5*((25.2+15+16.44+15.3+18.6)/5))
[1] 3.008987

Q3
> q2<-(3^2)*(4^(1/8))
> q2<-q2/2.33
> q2
[1] 4.593504

Q4
> q3 <- seq(from=5, to=-11, by=-0.3)
> q3 <- sort(x=q3, decreasing=FALSE)
> q3
[1] -10.9 -10.6 -10.3 -10.0 -9.7 -9.4 -9.1 -8.8 -8.5 -8.2 -7.9
[12] -7.6 -7.3 -7.0 -6.7 -6.4 -6.1 -5.8 -5.5 -5.2 -4.9 -4.6
[23] -4.3 -4.0 -3.7 -3.4 -3.1 -2.8 -2.5 -2.2 -1.9 -1.6 -1.3
[34] -1.0 -0.7 -0.4 -0.1 0.2 0.5 0.8 1.1 1.4 1.7 2.0
[45] 2.3 2.6 2.9 3.2 3.5 3.8 4.1 4.4 4.7 5.0

Q5
> bar <- rep(x=c(-1,3,-5,7,-9),times=2,each=10)
> sort(x=bar,decreasing=TRUE)
[1] 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 3 3 3 3 3
[26] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
[76] -5 -5 -5 -5 -5 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9

Q6
> q4 <- c(seq(from=3,to=6,length.out=5), rep(c(2,-5.1,-33),times=2), 7/42+2)
> q4
[1] 3.000000 3.750000 4.500000 5.250000 6.000000 2.000000 -5.100000 -33.000000
[11] 2.000000 -5.100000 -33.000000 2.166667
> qnew <- q4[c(1, length(x=q4))]
> qnew
[1] 3.000000 2.166667
Q7
> baz <- c(6:12,rep(5.3,times=3),-3)
> baz
[1] 6.0 7.0 8.0 9.0 10.0 11.0 12.0 5.3 5.3 5.3 -3.0

Q8
> q5 <- rep(x=c(2,4,6),times=2)*rep(x=c(1,2),each=3)
> q5
[1] 2 4 6 4 8 12

Q9
> y=1:100
> length(y)
[1] 100
> seq(from=102,to=length(y),length.out=9)
[1] 102.00 101.75 101.50 101.25 101.00 100.75 100.50 100.25 100.00

You might also like