Do the coding in R langauge and answers the following questions. Exercise 2.4 a.
ID: 3888462 • Letter: D
Question
Do the coding in R langauge and answers the following questions.
Exercise 2.4 a. 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) - -The value +2 b. Extract the first and last elements of your vector from (a), storing them as a new object. c. Store as a third object the values returned by omitting the first and last values of your vector from (a). Use only (b) and (c) to reconstruct (a) Overwrite (a) with the same values sorted from smallest to largest. Use the colon operator as an index vector to reverse the order of (e), and confirm this is identical to using sort on (e) with decreasing-TRUE. d. e. f. Create a vector from (c) that repeats the third element of (c) three times, the sixth element four times, and the last ele- ment once. g. h. Create a new vector as a copy of (e) by assigning (e) as is to a newly named object. Using this new copy of (e), overwrite the first, the fifth to the seventh (inclusive), and the last element with the values 99 to 95 (inclusive), respectively.Explanation / Answer
###Create a vector from 3 to 6 with length=5##
##Sequence length should be 5 but the difference between numbers in 1, so we keep the difference
#0.75 to generate 5 numbers in between
vec1<-seq(3,6,by=0.75)
##Result##
# vec1
# [1] 3.00 3.75 4.50 5.25 6.00
##Two fold repetition of vector##
vec2<-rep(c(2,-5.1,-33),2)
##Result##
# vec2
#[1] 2.0 -5.1 -33.0 2.0 -5.1 -33.0
## 7/42 +2
vec3<-(7/42)+2
##Result##
# vec3
# [1] 2.166667
##a) Combine 3 vectors##
answer_a<-c(vec1,vec2,vec3)
##Result##
# answer_a
# [1] 3.000000 3.750000 4.500000 5.250000 6.000000 2.000000 -5.100000 -33.000000 2.000000 -5.100000 -33.000000
# [12] 2.166667
###b) Extract the first and last elements of previous vector
answer_b<-c(answer_a[1],answer_a[length(answer_a)])
##Result##
# answer_b
# [1] 3.000000 2.166667
##c) Other than first and last element, rest of the elements
answer_c<-answer_a[-c(1,length(answer_a))]
##Result##
# answer_c
# [1] 3.75 4.50 5.25 6.00 2.00 -5.10 -33.00 2.00 -5.10 -33.00
##d)combine b and c to reconstuct a
answer_d<-c(answer_b[1],answer_c,answer_b[2])
##Result##
##answer_d
# [1] 3.000000 3.750000 4.500000 5.250000 6.000000 2.000000 -5.100000 -33.000000 2.000000 -5.100000 -33.000000
# [12] 2.16666
##e)Overwrite a in sorted order
answer_a<-sort(answer_a,decreasing = FALSE)
##Result##
# answer_a
# [1] -33.000000 -33.000000 -5.100000 -5.100000 2.000000 2.000000 2.166667 3.000000 3.750000 4.500000 5.250000
# [12] 6.000000
##f)Sort by colon and sort by sort operator and check if the opertion is same
##Sort by colon
answer_e1<-answer_a[length(answer_a):1]
##Sort by sort operator
answer_e2<-sort(answer_a,decreasing = TRUE)
##Compare the result
answer_e1==answer_e2
##Result
# > answer_e1==answer_e2
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
##g)Vector from c
answer_g<-c(rep(answer_c[3],3),rep(answer_c[6],4),rep(answer_c[length(answer_c)],1))
##Result
# > answer_g
# [1] 5.25 5.25 5.25 -5.10 -5.10 -5.10 -5.10 -33.00
##h)
##copy
answer_h<-answer_a
answer_h[c(1,5:7,length(answer_h))]<-seq(99,95,by=-1)
##Result
answer_h[c(1,5:7,length(answer_h))]<-seq(99,95,by=-1)
# > answer_h
# [1] 99.00 -33.00 -5.10 -5.10 98.00 97.00 96.00 3.00 3.75 4.50 5.25 95.0
#
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.