-1

I was able to create a function that allowed me to read data from a csv file and calculate the mean of specific columns.

My problem now is that when I try to combine data from multiple csv files using rbind in order to calculate the mean, it returns the error message in the picture. Please show me what I'm missing and where I have gone wrong.

enter image description here

Tung
  • 26,371
  • 7
  • 91
  • 115
Simpa
  • 1

2 Answers2

0

Please see the comments re reproducible sample data and code. If unsure, see how to provide a minimal reproducible example/attempt.

That aside, I would suggest doing something like this:

# Filenames
id <- 1:332;
g <- paste(sprintf("%03d", id), "csv", sep = ".")

# Read CSV files into list of data.frames and row-bind into single data.frame
library(tidyverse);
df <- bind_rows(lapply(g, read_csv), .id = "source");

Provided your CSV files all have the same structure, this will create a single data.frame with entries from all 332 CSV files; the additional column source tracks the origin of every entry (i.e. from which file it originated).

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

There are so many ways to merge data from multiple CSVs into a single data frame. Here are some examples of how to do that. I'll leave it up to you to get the mean (it should be pretty easy to do this).

setwd("C:/your_path_here")
fnames <- list.files()
csv <- lapply(fnames, read.csv)
result <- do.call(rbind, csv)

******** ******** ******** ******** ******** ******** ******** ******** ********

filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind,lapply(file_names,read.csv))

******** ******** ******** ******** ******** ******** ******** ******** ********

filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, skip = 1, header = FALSE))

******** ******** ******** ******** ******** ******** ******** ******** ********

filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, header = FALSE))

******** ******** ******** ******** ******** ******** ******** ******** ********

temp <- setwd("C:/your_path_here")
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)
ASH
  • 20,759
  • 19
  • 87
  • 200