for loop modification

Hi,

I read some csv files and do some data analyse.
My code is

numbers <- 2012:2018
for (z in numbers) {
filenames <- paste(paste("immunisation","ethnicity", z,
"6","months",sep="-"),".csv",sep="")
x <- seq(2,10,2)
y <- seq(1,10,2)
overallMax <- 0
for (i in filenames) {
file <- read.csv(i,row.names = 1)
immu_i <- file[,x]
imm_i <- sum(immu_i,na.rm = TRUE)
elig_i <- file[,y]
eli_i <- sum(elig_i,na.rm = TRUE)
prop_i <- round(imm_i/eli_i, digits = 2)
if (overallMax < prop_i) {
overallMax <- prop_i
message <- paste("The","highest","proportion","of","immunised",
"children","so","far","(",overallMax, ")","occurred",
"in", z,"\n")
cat(message)
}

}

}

My result is
The highest proportion of immunised children so far ( 0.74 ) occurred in 2012
The highest proportion of immunised children so far ( 0.77 ) occurred in 2013
The highest proportion of immunised children so far ( 0.79 ) occurred in 2014
The highest proportion of immunised children so far ( 0.8 ) occurred in 2015
The highest proportion of immunised children so far ( 0.81 ) occurred in 2016
The highest proportion of immunised children so far ( 0.79 ) occurred in 2017
The highest proportion of immunised children so far ( 0.78 ) occurred in 2018

Now I want to delete the 2017 and 2018 sentences because their proportion is lower than the previous year. I understand that I should use a for loop. I tried some ways but I haven't got the right one. Anyone can help me with that?

Thanks in advance

See what happens when you use overallMax <- 0 outside all for loops.

Wow, amazing!!
Thank you so much and it works as what I want it to do.

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