Hi everyone,
I am doing a loop to make some graphs. The problem is that May 2014 is not in my data. Hence I decided to do an if into my loops. Actually, I do know the loops & the if are working, but partially. All my may & my 2014 data are just eliminated, which is not my aims but only may 2014. I already spent a day on it. I am pretty sure I am just making a bad mistake (since it's my first if in R). I would like do it only if the both are combine.
Mymonths <- month.name
Myyears <- unique(HQ$Year)Myyears
[1] 2013 2014 2015 2016 2017 2018 2019Mymonths
[1] "January" "February" "March" "April" "May" "June" "July" "August" "September" "October"
[11] "November" "December"for (Y in Myyears) {
for (M in Mymonths) {
if(2014 != Y && "May" != M) {
print(Y)
print(M)
}
}
}
...
[1] "April"
[1] 2013
[1] "June"
[1] 2013
[1] "July"
[1] 2013
[1] "August"
[1] 2013
[1] "September"
[1] 2013
[1] "October"
[1] 2013
[1] "November"
[1] 2013
[1] "December"
[1] 2015
...
I already try all theses :
if(2014 != Y && "May" != M);
Myyears <- as.character(Myyears)
if("2014" != Y && "May" != M)if(Y != 2014 && M != "May")
if(Y != 2014 & M != "May")
All of the aboe give the same results.
I know that one way I could solve it is by doing another if combine to an ifesle, but I think that would be bad coding.
for (Y in Myyears) {
for (M in Mymonths) {
if("2014" != Y) {
print(Y)
print(M)
} else {
if("May" != M){
print(Y)
print(M)
}
}
}
}
[1] "April"
[1] "2013"
[1] "May"
[1] "2013"
[1] "June"
[1] "2013"
[1] "July"
[1] "2013"
[1] "August"
[1] "2013"
[1] "September"
[1] "2013"
[1] "October"
[1] "2013"
[1] "November"
[1] "2013"
[1] "December"
[1] "2014"
[1] "January"
[1] "2014"
As we can see the both if are working, both, it's not the best way to write if, especially if I had like 4 conditions.