How to read multiple cvs files at once & Calculate occurrence in each file

Thank you very much for reading my poster from a learning novice in R.

Here is my goal:
*I have 45 individual cvs files, each with 8 rows and 16 columns. Each cell contains number from 0 to 6 (e.g. 0, 0 , 1, 1, 5...)

*I need to get the number of occurrence of each number (from 0 to 6) in each file.

*Right now, I can get the number by opening individual files, and do that 45 times. BUT that is too laborious!


My lengthy R-code is as follows:

matrix<-matrixList1 #Then I have to change it from "matrixList1" to "matrixList45" for 45 times

#count number of 1
count1<-length(which(matrix==1))
#count real number of 1, excluding the eight "1" in the headline
count1
#average number of appearing once as a competitor
average1 <- count1/8

#count number of 2
count2<-length(which(matrix==2))
#count real number of 2, excluding the two "2" in the headline
count2
#average number of appearing twice as a competitor
average2 <- count2/8

#count number of 3
count3<-length(which(matrix==3))
#count real number of 3, excluding the two "3" in the headline
count3
#average number of appearing three times as a competitor
average3 <- count3/8

#count number of 4
count4<-length(which(matrix==4))
#count real number of 4, excluding the two "4" in the headline
count4
#average number of appearing four times as a competitor
average4 <- count4/8

#count number of 5
count5<-length(which(matrix==5))
#count real number of 5, excluding the two "5" in the headline
count5
#average number of appearing five times as a competitor
average5 <- count5/8

#count number of 6
count6<-length(which(matrix==6))
#count real number of 6, excluding the two "6" in the headline
count6
#average number of appearing fsix times as a word
average6 <- count6/8

#make life easier
table1 <- c(count1, count2, count3, count4, count5, count6)
table2<- c(average1, average2, average3, average4, average5, average6)
#print tables
table1
table2

Output goes like:

#print tables
table1
[1] 55 19 1 0 0 16
table2
[1] 6.875 2.375 0.125 0.000 0.000 2.000

It is satisfactory; But, STILL I need to do this 45 times in order to get a gist of all files.

*My question is:
How do read all csv files at one time, and get the occurrence I need from each list at once?

I would be very appreciate for you help!

1 Like

A reproducible example, called a reprex would allow a more specific answer.

Take a look at this post for one approach and the importCSV() function in the DataLoader package. The latter gives you "a single list of dataframes containing all the files imported and stored as dataframes."

1 Like

You could also follow this pattern to read all files at once into the same dataframe but differentiated by the source file name

library(tidyverse)

list_of_files <- list.files(path = "path/to/your/files",
                            pattern = "\\.csv$",
                            full.names = TRUE)
df <- list_of_files %>%
    set_names() %>%  
    map_dfr(read_csv, .id = "file_name")
3 Likes

Thank you a lot! :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.