I have a folder with many csv files in it. They are all structured as per picture
I am interested to count the numbers under my variable "Type" and get an output that tells me that there are two number 7, two number 9, two 1 and so on. I want to do this for the csv files in my folder, and it would be great to bind the outputs from different files together (with an identifier to the original file the output was extracted from). So far, I managed to do it for individual files with this code:
mydata <- read.csv("1_data.csv", skip=1, header = T)
df <- data.frame(table(mydata$Type))
However, I tried to code a loop and got stucked. This is the code I am using:
files = list.files(pattern = "*.csv")
for (i in files) {
id <- substr(i, 1, 5)
mydata <- read.csv (i, skip=1, header = T)
datatobind <- data.frame(table(mydata$Type))
datatobind["id"] <- as.numeric(id)
data <- rbind(data, datatobind)
}
do.call (rbind, data)
write.csv(data, file='final.csv', row.names=FALSE)
I get a different error every time I try to change the code, so I am not sure how to fix this.