"in" is not working in my For loop

I want to make a for loop with some functions I created earlier in my script

a <- read.csv("FP_2019.csv")
b <- read.csv("RH_2019.csv")
c <- read.csv("health_sector_2019.csv")

d <- read.csv("FP_2018.csv")
e <- read.csv("RH_2018.csv")
f <- read.csv("health_sector_2018.csv")

x <- read.csv("FP_2017.csv")
y <- read.csv("RH_2017.csv")
z <- read.csv("health_sector_2017.csv")

health <- c(c,f,z)
RH <- c(b,e,y)
FP <- c(a,d,x)

#loop the sectors

For(i in health) {
  data_format(i)
  wordsearch_health(i)
  filter_data_health(i)
  return(i)
}

This is what I have written and I will eventually have 3 loops one for health, RH and FP.

When I run the loop I get this error:

Error: unexpected 'in' in "For(i in"

Where can I go from here?

You need a lower-case "f" in "for", i.e. try:

for (i in health) {
  data_format(i)
  wordsearch_health(i)
  filter_data_health(i)
  return(i)
}
2 Likes

With c being an almost ubiquitous function name its less ideal to use single letter variable names in your work. Perhaps adding an underscore at the end would help, though a meaningful variable name is typically preferred.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.