Hello, I'm currently working with R for my studies and no matter what I do, the "&&" operator just doesn't want to work. How can I solve this problem?

I hope someone can help me.

Kind regards,

Nathalie

Hello, I'm currently working with R for my studies and no matter what I do, the "&&" operator just doesn't want to work. How can I solve this problem?

I hope someone can help me.

Kind regards,

Nathalie

Post the simplest example you can of your code that doesn't work. (Copy and paste, not a screenshot.)

TRUE && c(TRUE, FALSE, FALSE)

It's part of a learning program that's called Swirl.

Thank you for the fast reply.

`&&`

wants to compare two logical expressions. You have a logical expression and a vector. `&`

will compare two vectors.

```
> TRUE & c(TRUE, FALSE, FALSE)
[1] TRUE FALSE FALSE
```

Personally, I find this confusing too! You can read more at Difference Between & and && in R - GeeksforGeeks.

The behavior of && changed with R 4.3.0. From the documentation:

Using vectors of more than one element in `&&`

or `||`

will give an error.

2 Likes

Is there a way to turn on an "old" behaviour (like it was before 4.3.0) in 4.3.0 and 4.3.1 when it gave a warning, not an error ?

Long ago, before the Olympics left off being a sports event and turned into an entertainment spectacle, the figure skating competition had a mandatory event called `school figures.`

Instead of dazzling leaps and spins and high speed graceful flowing sweeps across the rink at speed, there was skating figure-8s and circles repeatedly. Sort of what a Tibetan meditator on skates would do. I was captivated by watching Peggy Fleming do those in winning her 1968 Gold Medal. She made something difficult (cutting perfect circles in ice with your foot) look effortless.

Boolean algebra is like that. Very simple but also very easy to fall down while trying to chew gum at the same time. In time, it's possible to develop an intuition for the simple cases, but putting the power of the tool to use requires being systematic. It becomes the *one stupid trick* that is incredibly useful in much of routine work in `R`

.

Let's look at the `mtcars`

dataset in Boolean terms. Suppose, for some silly reason, I want to find the cars that have miles per gallon of at least 30 and displacement less than 100 plus either 4 cylinders or 5 gears, but not both.

```
mtcars[which(mtcars$mpg >= 30 & mtcars$disp < 100 &
xor(mtcars$carb == 4,mtcars$gear == 5)),]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
```

Now, let's change it slightly so that the last condition is either 4 cylinders or 4 gears, but not both

```
mtcars[which(mtcars$mpg >= 30 & mtcars$disp < 100 &
xor(mtcars$carb == 4,mtcars$gear == 4)),]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
```

^{Created on 2023-08-14 with reprex v2.0.2}

Doing these types of exercises is the best way I know to get comfortable using simple language features that become murky as soon as a bit of complexity is added.

I would also like to know this please, as I would need it to get through my Swirl Course.

If you want core behaviour from an earlier release, its best to simply adopt that release for that project. R Studio IDE, will let you change the R backend on a per project basis. so if you want R.4.2.1 or something similar, download it and use it. To get through this course, I recommend the user download such a version of R as is most compatible with the course she is attempting to complete.

How do I know which one is the best?

swirl was last updated in Jan 2020; looking down the list of R versions.

-project

R3.6.2 I would think is good.

It may be a good idea, to go through swirl twice; once with this 3.6.2; then again with R 4.3.0 noticing the differences (i.e. &&)

Also, it seems you are not alone; the bugs/issues have been logged here :

Thank you so much for the help! I will try it!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.