This code is not getting executed either in R editor or R console, any guidance is appreciated
data("airquality")
View(airquality)
ozone_level <- airquality[1,"Ozone"]
if is.na(ozone_level)
{print("ozone_level reading is missing.")}
else if (ozone_level<30){
print("low ozone level.")
} else{}
print("high ozone level.")}
1 Like
Try this
data("airquality")
View(airquality)
ozone_level <- airquality[1,"Ozone"]
is.na(ozone_level)
{print("ozone_level reading is missing.")}
if (ozone_level<30){
print("low ozone level.")
} else{
print("high ozone level.")}
You had an extra } in there and I don't think you can do an if()
at the start of the expression with an is'na
.
I think you meant to write:
if (is.na(ozone_level)) {
print("ozone_level reading is missing.")
} else {
if (ozone_level < 30) {
print("low ozone level.")
} else {
print("high ozone level.")
}
}
1 Like
Thanks for the help, I ran your code and I get this error - unexpected '}' in " print("high ozone level.")}" unable to figure where is the additional {
This code works, I did not know the entire else will be nested till the end. Was that the error? or something else. Thanks for your help.
1 Like
Your if
statement syntax is incorrect. It should be:
data("airquality")
View(airquality)
ozone_level <- airquality[1, "Ozone"]
if (is.na(ozone_level)) {
print("ozone_level reading is missing.")
} else if (ozone_level < 30) {
print("low ozone level.")
} else {
print("high ozone level.")
}
Fix the missing parentheses in if is.na(ozone_level)
, and remove the extra }
at the end. This should run properly in both the R editor and console! 