Ch. 5: Tasks & Quizzes
Q5.1 What happens if you type a word without quotation marks in the Console and then press “enter”?
Q5.2 Try entering 13^2 in the Console. What does the ^ (caret) operator do?
Q5.3 Compare 13*13 with 13 * 13. What is the difference in the output?
Q5.4 Which of these object names are not allowed in R? Try to create an object with each of these names and see if you get an error message or not.
Q5.5 What is the relational operator that checks whether a value is “more than or equal to” another value?
Q5.6 What is the relational operator that checks whether a value “is not equal to” another value?
Above, we created the following R character string object:
New.French.Word <- "écogeste"Q5.7 Why does this line of code return FALSE even though New.French.Word was assigned the character string “écogeste”?
New.French.Word == "ecogeste"Q5.8 Why does this line of code return FALSE?
New.French.Word == " écogeste"Q5.9 Why does this line of code return FALSE?
New.French.Word == "Écogeste"Q5.10 Why does this line of code return FALSE?
New.French.Word != "écogeste"Q5.11 Copy and paste the following lines of code in a new .R script. Try to run each line individually. Each line will generate an error of some kind. In some cases, RStudio will warn you in advance that a line of code is likely wrong by displaying a red cross icon to the left of the erroneous line. If you hover over the red cross icon, RStudio will display a message that may help you to fix the error.
Can you decode the error messages to find out what is causing these errors and fix these ten erroneous commands?
my.favourite.word <- "empathy"
my.favourite.number <- 13
# Error 1:
my.favourite.number + my.favorite.number
# Error 2:
Negin-Fav-Word <- "Ach so!"
# Error 3:
my.favourite.numbers^2
# Error 4:
ömers_favourite_ number <- 52
# Error 5:
ömers_favorite_number = my.favourite..number
# Error 6:
my.favourite.number*2 -> half.my.fav.number
# Error 7:
rose's.favourite.number <- 5
# Error 8:
BestWordEver <- "supercalifragilisticexpialidocious
# Error 9:
2FavNumbers <- my.favourite.number + ömers_favourite_number
# Error 10:
good.luck <- موفق باشيد"The first error was
object 'my.favorite.number' not found. This means that the objectmy.favorite.numberis not stored in your environment. If you think it is, the problem is most likely due to a typo. Here,my.favorite.numberuses American English spelling, whereas we used British English spelling (with a “u”) when we created the object. To correct the error, you need to use exactly the same spelling as when you created the object.The second error is also
object 'Negin' not found. However, here we do not expect an object calledNeginto be in the environment because what we are actually trying to do is create and save a new object calledNegin-Fav-Word! The problem is thatRinterprets the hyphens in this object name as “minus” and therefore tries to find the objectNeginin order to then subtractFavandWordfrom it. To correct this error, you need to remove the hyphens or replace them by dots.The third error is yet another
object not found error. It is another typo: the correct object name is not in the plural form.The fourth error is
Error: unexpected symbol in "ömers_favourite_ number". In addition, RStudio warned us that there were some “unexpected tokens” in this line of code. The unexpected item is the space between_andnumber. To fix this error, you need to remove this space character.The object
my.favourite..numberis not found because the name of the object saved in the environment does not have two consecutive dots. Note that the error does not come from the fact that this line begins with some white space and includes multiple space characters after the=sign. These added spaces make the line more difficult for us humans to read, butRsimply ignores them. Hence, to fix this error, what you need to do is remove one of the consecutive dots in the object name.It is also worth noting that, once you’ve removed the extra dot, this line of code replaces the value originally stored in
ömers_favourite_numberwith the value stored inmy.favourite.number. If you check your environment pane, you will see that the command has changedömers_favourite_numberto13with no warning!! In other words, here, the equal sign=behaves in the same way as the assignment operator<-.If you tried to run this line, you will have noticed that it does not actually generate an error. However, you may have noticed that the assignment operator is in the opposite direction (
->instead of<-). This means thatmy.favourite.numbertimes two is assigned to a new object calledhalf.my.fav.number. Having observed this, you will hopefully want to either amend the line for it to make mathematical sense (my.favourite.number/2 -> half.my.fav.number) or change the name of the new object for it to be meaningful (my.favourite.number*2 -> twice.my.fav.number).Running this line will have caused you to run into a
+situation in the console. As explained in Dealing with errors 🤬, to get out of it, first take your mouse cursor to the Console pane and press the Escape key on your keyboard to cancel the erroneous line. Whilst there is no error message to help you understand where the problem is coming from, you should see that RStudio helpfully displays a red cross icon to the left of the line; hovering over it displays a multi-line message. The first line is the relevant one:unexpected token 's.favourite.number <- 5. This tells us that apostrophes are forbidden in object names. Remove the'and the error will be fixed.This line also causes a
+situation. In this case, it is due to a missing quotation mark. To fix this error, first cancel the incomplete line of code by escaping it. Then, add the missing double quotation mark in your script and rerun the completed line.The message
Error: unexpected symbol in "2FavNumbers"is due to the fact that object names cannot start with a number. Change the object name to something likeTwoFavNumbersorFav2Numbersto fix this error.Here, too, the error message reads:
unexpected symbol. However, it is important to remember that the unexpected symbol is not within the character string, but rather has to do with the code syntax used to assign the Persian string togood.luck. In other words, the problem has nothing to do with the fact that the string is in Persian, but rather that one of the quotation marks is missing. You can fix the error by ensuring that the phrase is enclosed in quotation marks.
Check your progress 🌟
You have successfully completed 0 out of 11 questions in this chapter.