Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem. This problem concerns accessing data using the R-software package. Cons

ID: 3046690 • Letter: P

Question

Problem. This problem concerns accessing data using the R-software package. Consider the following data vector, Zlata = [1, 3, 3, 5, 4, 4, 6, 2, 8, 7]. Nrite down the simple R-software code lines needed to accomplish the following tasks: a) compute the length of data; b) select the 5-th element in idata; c) select the first three elements in Tata d) select the last four elements in idata e) select the first, the second, and the seventh elements in idata f) find all the elements in lata that are greater than 4; g) find all elements ay in a-data such that 3

Explanation / Answer

x<-c(1,3,3,5,4,4,6,2,8,7)

a.
## To get the length of the data, function "length" is used as:

length(x)

10

Therefore, length of the data is 10

b.

For the purpose of selection this "[ ]" operator is used. In this question we have to select the 5th element. For this we simply write the object name (i.e. x) then inside the square bracket we give the desired element which
we need to extract as:

x[5] ## Since we need 5th element

# this will give

4
Therefore, the 5th element is 4

c.

Here, we have to extract the first three element, the command is,

x[c(1:3)]

or

x[c(1,2,3)]

# the both will give

1 3 3

Therefore, the first three element is 1,3,3

d.

R code to extract last 4 digits is,

x[c(7:10)]

or

x[c(7,8,9,10)]

# the both will give

6 2 8 7

Therefore, the last four digit is 6,2,8,7

e.

To select the first, second and seventh element, R code is:

x[c(1,2,7)]

# this will give

1 3 6

Therefore, the first, second and seventh element is 1,3,6, respectively.

f.

R code for the element greater than 4 is done by using function "subset" as

subset(x, x>4) # this will give

5 6 7 8

Therefore, the element greater than 4 is 5,6,7,8

g.

3<x<7 can also be written as x>3 & x<7, this will the element between greater than 3 and less than 7, which is our desired result

subset(x, x>3 & x<7) ## this will give

5 4 4 6

Therefore, the elements which lies between greater than 3 and less than 7 is 5,4,4,6

h.

First, we need to know the greatest numerical value, for this R code is,
max(x) # this will give the greatest value

8

Therefore, 8 is the greatest numerical value.

Now, we have to find the position of 8, for this function "which" is used as,

which(c(1,3,3,5,4,4,6,2,8,7)==8)

or

which(c(x)==8)

The both will give

9 # i.e. the position of element 8 is 9

Therefore, the greatest numerical value 8 is located at 9th place.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote