0

When in swirl, I got the following issue:

Now we'll type the same expression except we'll use the `&&` operator. Type
the expression TRUE && c(TRUE, FALSE, FALSE).

> TRUE && c(TRUE, FALSE, FALSE)
Error in TRUE && c(TRUE, FALSE, FALSE) : 
  'length = 3' in coercion to 'logical(1)'

Having trouble finding the cause for this problem.

Tried using if() any() and all() but just says that it's not what it is looking for.

  • That statement should return a warning. The `&&` operator is not vectorized. Maybe your swirl lesson is out of date or incorrect. If you run `TRUE & c(TRUE, FALSE, FALSE)` you don't get a warning because `&` is vectorized. How exactly can we re-create this problem so we can test it on our own? What commands did you run to get to this point? – MrFlick May 17 '23 at 14:19
  • I am using version 4.3 and I am very new at this. I have just been doing the first 8 lessons of the R Programming Logic the past 24 hours and everything so far has gone smoothly. I looked up the errors and found some things saying to place the vector inside any() or all() but it just responds saying "Keep Trying." Maybe I should download an older version? Let me know what to add to help. Thanks. – markerbrecht May 17 '23 at 14:26
  • 3
    R has become more strict about logical vector recycling. Maybe it would work in an older version of R? According to [this](https://github.com/swirldev/swirl_courses/blob/201ccea9d40bd16a3ffa77e5c01e795880793116/R_Programming/Logic/lesson.yaml#LL147C18-L147C48), you are typing what it expects but this is considered an error now and it wasn't before. It seems that error has already been reported: https://github.com/swirldev/swirl_courses/issues/522 – MrFlick May 17 '23 at 14:30

1 Answers1

0

The swirl lesson hasn't been updated since the change in R 4.3.0 where '&&' no longer accepts a vector of length > 1. Previously, as indicated in Swirl, the first element of the vector would be assessed, now it returns an error. The same is true for the non-vectorised OR '||'

You can't get passed this question with the lesson as it is written. I have put in a pull request on github for it to be fixed.

In the meantime, you can either directly edit the lesson .yaml file (i.e. find and delete the '&&' and '||' questions), the file is at:

file.path(find.package("swirl"),"Courses","R_Programming","Logic","lesson.yaml")

Alternatively you can download the lesson I have editted for the pull request by running these three lines (you may need to start swirl with a different nickname when it asks):

# create file path to swirl lesson file
file_path <- file.path(find.package("swirl"),"Courses","R_Programming","Logic","lesson.yaml")
# url of my edited lesson file
file_url <- "https://raw.githubusercontent.com/daniel-j-miller/swirl_courses/master/R_Programming/Logic/lesson.yaml"

# download and save file
download.file(file_url, file_path)