I am working on importing multiple csv files which contain data from climate sensors (temp, humidity, etc.). My code is below. First, I get a list of all the files I am importing and the length of that list. I then have a for loop cycling through each file, inside of that an if loop checking if the file is a csv. In the if loop, the code reads the file in and temporarily stores it in "store". Afterwards, a data frame which holds all of the data, called "main", and "store" are combined using rbind.
file_list <- list.files(".")
num_files <- length(file_list)
main <- data.frame()
for (i in 1:num_files){
check <- stri_length(file_list[i])
if (substr(file_list[i],start = check - 2, stop = check) == "csv"){
store <- read.csv(file_list[i])
main <- rbind(main, store)
}
However, when I run the code, I get the following error: "Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match"
There are a few things that are interesting with this code and error. First, when I run this code on a windows computer, it works brilliantly with no error, but it does not work on my MAC. Second, all the columns in the imported data are identical, so rbind should work, and it does on a windows. Third, when I examine the empty data frame "main," it has very weird column names that do not match up with the data, for example, X21092, X16, X5.2, etc... Hence, it will not bind the temporarily stored data and the empty data frame. Now, when I do not use rbind and try something different such as bind_rows, I get the same wacky column names that do not match up with anything, so the problem is not with rbind. Finally, when I run codes that use a very similar for loop with rbind on my MAC, they work just as expected.
I have tried clearing everything, such as the environment, using the code on a new file, and uninstalling R and RStudio, all of which yield the same result and error.
Is the some reason I am getting this error on my MAC and what should I do to fix it?
Here is a photo of the data frame when it runs correctly enter image description here
Here is an image of the data frame when I get the error on my MAC enter image description here