the condition has length > 1 and only the first element will be used - in R [closed]

Solution 1:

I tried to reproduce your example. I believe that the warning is raised whenever you have more than one .xlsx file in temp_dir. If so, then fileovi is a vector of strings, with as many elements as .xlsx files in temp_dir.

Thus, file_ext(fileovi) will be another vector of strings with the same number of elements. Passing it in the if condition raises the warning, since only the first element will be used by R to check the statement == "xlsx".

EDIT

In the comments, I suggestedd dropping |\\.xls$ in the definition of fileovi, so to look for .xlsx files only. However, it has been correctly pointed out that the loop would crash when operating over a folder with no such files.

To solve this, I suggest to modify the if condition before the inner loop, using if (!identical(fileovi, character(0))) in such a way that the code below runs only if we found at least one .xlsx file. Following, my proposal:

library(readxl)

putanja = "Z:/Radne Skupine/Reports/AK-ID"

direktoriji <- list.dirs(putanja, full.names = TRUE,recursive = FALSE)

for (d in 1:length(direktoriji))
{
  temp_dir = direktoriji[d]
  fileovi <- list.files(path=temp_dir,pattern = "\\.xlsx$") # LOOKING FOR .XLSX ONLY!
  if (!identical(fileovi, character(0))) # IF NO .XLSX FILE, SKIP THE FOLDER!
  { 
    for (f in 1:length(fileovi))
    {
      temp_file = paste(direktoriji[d],fileovi[f],sep="/")
      # IF 01.00
      temp1 <- read_excel(path = temp_file,sheet = "IF 01.00",range = "A4:C56")
      # IF 02.01
      temp2 <- read_excel(path = temp_file,sheet = "IF 02.00",range = "A4:C19")
      # IF 02.02
      temp3 <- read_excel(path = temp_file,sheet = "IF 02.00",range = "A28:C35")
      # IF 03.00
      temp4 <- read_excel(path = temp_file,sheet = "IF 03.00",range = "A4:C25")
      # IF 04.00
      temp5 <- read_excel(path = temp_file,sheet = "IF 04.00",range = "A3:C20")
      # IF 05.00
      temp6 <- read_excel(path = temp_file,sheet = "IF 05.00",range = "A4:C33")
      # IF 06.01
      temp7 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A6:E12")
      # IF 06.02
      temp8 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A16:P22")
      # IF 06.03
      temp9 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A26:E30")
      # IF 06.04
      temp10 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A34:J38")
      # IF 06.05
      temp11 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A43:E50")
      # IF 06.06
      temp12 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A55:J62")
      # IF 06.07
      temp13 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A67:E75")
      # IF 06.08
      temp14 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A79:G87")
      # IF 06.09
      temp15 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A92:C102")
      # IF 06.10
      temp16 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "B107:G110")
      # IF 06.11
      temp17 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A115:G129")
      # IF 06.12
      temp18 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A134:E138")
      # IF 06.13
      temp19 <- read_excel(path = temp_file,sheet = "IF 06.00",range = "A143:J147")
      
      # sklapanje
      temp <- rbind(temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,temp9,temp10,temp11,temp12,temp13,temp14,temp15,temp16,temp17,temp18,temp19)
    }
  }
}