FAQ: Introduction to R Syntax - Logical Operators

Your message must match exactly what the instructions say to pass the exercise. That includes case and punctuation. So either, “Pack umbrella!” or “No need for umbrella!”.
                                                ^                      ^        ^                                 ^

Im getting the same sort of error as others here. Can someone take a look and see where ive gone wrong please

Hey, I’ve got the same problem as many of you. code runs, but no checkbox.

message <- 'Should I pack an umbrella?'
weather <- 'cloudy'
high_chance_of_rain <- TRUE
if (weather == 'cloudy' & high_chance_of_rain) {
  message <- "Pack umbrella"
} else {
  message <- "No need for umbrella!"
}
print(message)

got my coding right but got an error message .when I checked the solution I realized i had to give 2 pacing from the skeleton coding on the coding note book.

Hello everyone. I got the code right but I’m confused about what the last question means.
"Based on the condition, what should its value be?"
Can someone explain this question?
Thank you very much.

So the only thing I got wrong was I used double quotes instead of single quotes. Up until now the exercises have allowed for either, but this exercise’s logic marks it correct only if single quotes are used and the “<-” is used to assign Message instead of using “=”, even though in the first lesson it said both are acceptable and I’ve used “=” so far and the lessons all marked it correct. Hopefully this can get fixed.

I wasn’t sure why I had to write message instead of print when I wrote Pack umbrella!

When should we be using double quotes instead of single quotes and how does R reads them in different contexts? Thx

1 Like

Hello, I would like to what does the semicolon (:wink: contribute to in the example explaining the AND logical operator? I omitted the semicolon (:wink: in and could not get the correct answer but noticed that the OR logical operator example did not have the semicolon (;). Hope to hear back! Thank you!

AND operator example
if (stopLight == ‘green’ & pedestrians == 0) {
print(‘Go!’);
} else {
print(‘Stop’);
}

OR operator example
if (day == ‘Saturday’ | day == ‘Sunday’) {
print(‘Enjoy the weekend!’)
} else {
print(‘Do some work.’)
}

HI there
first thanks for this cummunity

im getting error. please help.
Error in eval(ei, envir) : object ‘cluody’ not found

Can anyone tell me why i can’t write this way

if (weather == ‘cloudy’ & high_chance_of_rain == TURE)

why i can’t write high_chance_of_rain == TURE here?

First off, the spelling is incorrect. More importantly, you can write that but it is unnecessary to be that verbose.

if (high_chance_of_rain)

is testing for truthiness. If the truth value of ‘high_chance_of_rain’ is TRUE, it will be detected in the IF statement.

thanks. i am still a little confused. if (high_chance_of_rain) itself could be false or true, right? in that case, how we can assume if (high_chance_of_rain) will return true, not false?
it defines high_chance_of_rain ← true in the condition, so it is true by default? then why weather need to be = cloudy, why not writing it like this : if (weather, high_chance_of_rain)?

thanks!!!

Of all the possible objects, strings, numbers, etc. most are truthy, and only a few are falsy. I don’t know R, but suspect it to be much the same as other languages in this regard.

a -> ''

The ‘empty string’ is falsy.

a -> 0

Zero is falsy.

If R has a value for nil or null or undefined or none, they too are falsy. This is what is meant by ‘truth value’.

Now assuming high_chance_of_rain' is FALSE`, 0, ‘’, or any other falsy value, the if statement will branch to the ELSE clause if one exists, else move on to the next statement in the code.

Suggest locate the documentation for R and look for a topic on TRUTH VALUE which we assume will be in the literature somewhere. Don’t be afraid to search and dig a little. It makes for good practice and improves one’s familiarity with the documentation.

1 Like