Please code in R: Create and print a vector that contains the following in this
ID: 3045457 • Letter: P
Question
Please code in R:
Create and print a vector that contains the following in this order:
A sequence of integers from 6 to 10 (inclusive).
A twofold repetition of the vector c(2, -5.1, -33).
The value of the sum of 7/42 and 2.
1- Extract the first and last elements of the vector you have created to form another vector using the extracted elements. Print this second vector.
2- Form a third vector from the elements not extracted in (a). Print this vector.
3- Use the vectors from (a) and (b) to reconstruct and print the original first vector.
Explanation / Answer
a=c(6:10)
> b=rep(c(2, -5.1, -33),times=2)
> a
[1] 6 7 8 9 10
> b
[1] 2.0 -5.1 -33.0 2.0 -5.1 -33.0
> c=(7/42) + 2
> c
[1] 2.166667
> d=c(a,b,c)
> d
[1] 6.000000 7.000000 8.000000 9.000000 10.000000 2.000000
[7] -5.100000 -33.000000 2.000000 -5.100000 -33.000000 2.166667
> head(d,n=1)
[1] 6
> h=head(d,n=1)
> h
[1] 6
> t=tail(d,n=1)
> t
[1] 2.166667
> e=c(h,t)
> e
[1] 6.000000 2.166667
> length(d)
[1] 12
> f=d[2:11]
> f
[1] 7.0 8.0 9.0 10.0 2.0 -5.1 -33.0 2.0 -5.1 -33.0
> g=c(e,f)
> g
[1] 6.000000 2.166667 7.000000 8.000000 9.000000 10.000000
[7] 2.000000 -5.100000 -33.000000 2.000000 -5.100000 -33.000000
>