Мой R скрипт не собирает все файлы в папке - PullRequest
0 голосов
/ 20 февраля 2020

Мой сценарий R пытается объединить электронные таблицы Excel, которые находятся в разных папках в папке «Заинтересованные файлы» (показано в каталоге ниже), и поместить все данные в один главный файл. Тем не менее, сценарий случайным образом выбирает файлы для копирования информации, и когда я запускаю код, появляется следующая ошибка, поэтому я предполагаю, что именно поэтому он не выбирает каждый файл в папке?

all_some_data <- rbind(all_some_data, temp) 
Error in rbind(deparse.level, ...) : 
numbers of columns of arguments do not match

Весь код:

#list of people's name it has to search the folders for. For our purposes, i am only taking one name
managers <- c("Name")
#directory of all the files
directory = 'C:/Users/Username/OneDrive/Desktop/Testing/Concerned Files/'

#Create an empty dataframe
all_HR_data <-
setNames(
data.frame(matrix(ncol = 8, nrow = 0)),
c("Employee", "ID", "Overtime", "Regular", "Total", "Start", "End", "Manager")
)
str(files)
#loop through managers to get time sheets and then add file to combined dataframe
for (i in managers){
#a path to find all the extract files
 files <-
 list.files(
  path = paste(directory, i, "/", sep = ""),
  pattern = "*.xls",
  full.names = FALSE,
  recursive = FALSE
    )

   #for each file, get a start and end date of period, remove unnecessary columns, rename columns and add manager name
for (j in files){
temp <- read_excel(paste(directory, i, "/", j, sep = ""), skip = 8)

 #a bunch of manipulations with the data being copied over. Code not relevant to the problem

all_some_data <- rbind(all_some_data, temp)
     }
}

1 Ответ

1 голос
/ 25 февраля 2020

Наиболее вероятная причина вашей проблемы - дополнительный столбец в одном или нескольких ваших файлах.

Потенциальное решение наряду с повышением производительности заключается в использовании функции bind_rows из пакета dplyr. Эта функция более отказоустойчива, чем базовая R rbind.

Оберните вас l oop оператором lapply и затем используйте bind_rows для всего списка фреймов данных в одном операторе.

output <-lapply(files, function(j) {
      temp <- read_excel(paste(directory, i, "/", j, sep = ""), skip = 8)

       #a bunch of manipulations with the data being copied over. 
       # Code not relevant to the problem

      temp  #this is the returned value to the list
  })
all_some_data <- dplyr::bind_rows(output)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...