R: Trouble in swirl when using &&

1.7k Views Asked by At

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.

2

There are 2 best solutions below

0
Daniel Miller On

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)
0
NinjaS On

*Maybe obvious to others? but I had to add file.edit to make @Daniel Miller's suggestion to work.

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