-1

I made an anonymous function to select the first element of the vector Then to select the last element of the vector. When I selected the last element, it said Incorrect. When I selected the second element, it said correct. I am tracking R is 1 based. NOT zero based. So when I select the last element of the vector should it not be x[3]???

The first part: My function, what I used to select the first element per the problem. | Try using evaluate() along with an anonymous function to return the first element of | the vector c(8, 4, 0). Your anonymous function should only take one argument which | should be a variable x.

evaluate(function(x){x[1]}, c(8, 4, 0)) [1] 8

| You got it!

The second part of the problem: Select the last element of the vector

| Now try using evaluate() along with an anonymous function to return the last element | of the vector c(8, 4, 0). Your anonymous function should only take one argument which | should be a variable x.

evaluate(function(x){x[3]}, c(8, 4, 0)) [1] 0

| That's not exactly what I'm looking for. Try again. Or, type info() for more options.

| You may need to recall how to index vector elements. Remember that your anonymous | function should only have one argument, and that argument should be named x. Using | the length() function in your anonymous function may help you.

evaluate(function(x){x[2]}, c(8, 4, 0)) [1] 4

| You are really on a roll!

What the heck, I had to use x[2] in order to select the last element of the vector. Why is it not x[3]?

I am using Swirl right now in R.

  • Is this from a particular swirl course? Maybe the course is just wrong? Can you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with the commands you ran to run this swirl lession? – MrFlick May 30 '20 at 03:00

1 Answers1

0

What Swirl expected was to use the length() argument to find the last element in the vector, as in:

evaluate(function(x){x[length(x)]}, c(8, 4, 0))

This exercise is in module 9 of the R programming swirl course. About 2/3 through the lesson, I replicate the correct answer.

enter image description here

Len Greski
  • 10,505
  • 2
  • 22
  • 33
  • But if it worked with `x[2]` it must not be actually looking at the code. Why would it work with `x[2]`? – MrFlick May 30 '20 at 03:00
  • @MrFlick - Without debugging this further, I can't explain the information posted in the original question. I have demonstrated, however, that my answer is correct. – Len Greski May 30 '20 at 03:18
  • OK. It's just a bad test. When checking if the answer is right, it uses a vector that has length 2: https://github.com/swirldev/swirl_courses/blob/ac5c6142b7a51698c16fe8587222284779d66122/R_Programming/Functions/customTests.R#L101 So the reason why `x[2]` works is just a bit of random luck. – MrFlick May 30 '20 at 03:31
  • Hi Len, Thanks. I am just wondering why x[2] selects the last element of the vector but x[1] selects the first element in the vector. R is a 1 based index? do you suspect it is an error with the Swirl program? – wRt610n12 May 31 '20 at 20:51
  • You're welcome. Based on the comment from @MrFlick , it does appear to be an error in the Swirl program. – Len Greski May 31 '20 at 23:22