For loop combine with 2 if conditions. (only if).

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 2019

Mymonths
[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.

Have you tried formatting your data as a data.frame? Then let R handle the messy details like there are no rows for May 2014 when plotting or summarizing.

Yes, actually this is just a easy way of underlining my problem.
That is my real graphs :

for (Y in Myyears) {
for (M in Mymonths) {
if(2014 != Y) {
x <- ggplot(D_H_M_Y[D_H_M_Y$Month == M & D_H_M_Y$Year == Y,], aes(x=Hour,group =1))+
geom_smooth(aes(y=Consumption), se = FALSE, color = ConsumptionColor, size = 1.1)+
geom_line(aes(y=WPcof),color = WindColor, size = 1.1)+
scale_y_continuous(
name = "Consumption",
sec.axis=sec_axis(~./cof, name="Wind Power"))+
facet_wrap(Year~Month~Day, nrow=4)+
theme(
axis.title.y = element_text(color = ConsumptionColor, size =13),
axis.title.y.right = element_text(color = WindColor, size =13),
panel.grid=element_blank(),
axis.text.x = element_text(angle = 90, hjust = 1))
} else {
if("May" != M){
x <- ggplot(D_H_M_Y[D_H_M_Y$Month == M & D_H_M_Y$Year == Y,], aes(x=Hour,group =1))+
geom_smooth(aes(y=Consumption), se = FALSE, color = ConsumptionColor, size = 1.1)+
geom_line(aes(y=WP
cof),color = WindColor, size = 1.1)+
scale_y_continuous(
name = "Consumption",
sec.axis=sec_axis(~./cof, name="Wind Power"))+
facet_wrap(Year~Month~Day, nrow=4)+
theme(
axis.title.y = element_text(color = ConsumptionColor, size =13),
axis.title.y.right = element_text(color = WindColor, size =13),
panel.grid=element_blank(),
axis.text.x = element_text(angle = 90, hjust = 1))
print(x)
}
}
}
}

The thing is that I want to look at each observation, like for each months at a give time.
So when I loop & I say when it's may 2014 (and may 2014 is not in the data), the data.frame don't seems to solve this problem. However, for agragate it worked well.

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