0

I have a for loop that goes through a specific column in different CSV files (all these different files are just different runs for a specific class) and retrieve the count of each value. For example, in the first file (first run):

  0   1  67 
101 622 277

In the second run:

  0   1  67  68 
109 592 297   2 

In the third run:

  0   1  67 
114 640 246 

Note that each run might result in different values (look at the second run that includes one more value that is 68). I would like to merge all these results in one list and then write it to a CSV file. To do that, I did the following:

files <- list.files("/home/adam/Desktop/runs", pattern="*.csv", recursive=TRUE, full.names=TRUE, include.dirs=TRUE)
all <- list()
col <- 14
for(j in 1:length(files)){
  dataset <- read.csv(files[j])
  uniqueValues <- table(dataset[,col]) #this generates the examples shown above
  all <- rbind(uniqueValues)
}
write.table(all, "all.csv", col.names=TRUE, sep=",")

The result of all is:

  0   1  67 
114 640 246 

How to solve that?

The expected results in:

  0   1  67   68
101 622 277   0
109 592 297   2
114 640 246   0
Jaap
  • 81,064
  • 34
  • 182
  • 193
Adam Amin
  • 1,406
  • 2
  • 11
  • 23

1 Answers1

-2

Marked this as a potential duplicate see link here

library(plyr)

df1 <- data.frame(A0 = c(101),
              A1 = c(622),
              A67 = c(277))          
df2 <- data.frame(A0 = c(109),
              A1 = c(592),
              A67 = c(297),
              A68= c(2))  
df3 <- data.frame(A0 = c(114),
              A1 = c(640),
              A67 = c(246))  


newds=rbind.fill(df1,df2,df3)
Schilker
  • 505
  • 2
  • 11