0

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

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 4
    Can we get an example of the top of the df in Windows and in Mac? My first guess based on column names "X16" is that it seems like read.csv is not able to read in the header of the csv. But I'd need more info to see why. It could be a character issue, a whitespace issue, or something else entirely. – Adam Sampson Jun 29 '21 at 21:07
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. It seems unlikely that the CSV files are identical. There looks to be differences in the header which may be caused by encoding differences in the files. – MrFlick Jun 29 '21 at 21:22
  • I took a look at the headers of the data frames. I was able to eliminate the X in the front by including "check.names = F" in the read.csv function. However, the number of headers, header names, and the data within the data frame are still messed up. Is there any other reason the read.csv could be causing this issue only on my Mac? – GosHawk108 Jun 30 '21 at 21:16

0 Answers0