简体   繁体   中英

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

putanja <- "Z:/Radne Skupine/Reports/AK-ID"
    direktoriji <- list.dirs(putanja, full.names = TRUE,recursive = FALSE)
    direktoriji
    for (d in 1:length(direktoriji)){
      temp_dir = direktoriji[d]
      fileovi <- list.files(path=temp_dir,pattern = "\\.xlsx$|\\.xls$")
      if (file_ext(fileovi) == "xlsx") {
    
      #fileovi = fileovi %>% filter(file_ext(fileovi[fileovi!="\\.xls"]))
      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) #%>% mutate(datum=,subjekt=) 
        
        }
      
      }
    }

Hi, I am using this piece of code to make a procedure that goes through directories and reads.xlsx files. In directories there are also.xls files that I want to avoid. This piece of code do not get an error just warnings and they are all the same "the condition has length > 1 and only the first element will be used". Can you please help out?

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" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM