RHatesMe
RHatesMe

Reputation: 143

How Can I Merge Multiple Files with Unequal Rows in R

I have about 100 text files with two columns and about 320-500 rows I would like to merge into a single .csv file.

For example, I have file A that looks like this
A B
1 100
2 200
3 300
4 400

and File B looks like this
A B
1 100
2 200
3 300
4 400
5 300
6 400

However, when I enter this code into R: write.csv (data_list, ("file.csv"), row.names = FALSE, na=""),
I get this error message: "Error in data.frame(list(V1 = c(0.025, 0.035, 0.045, 0.055, 0.065, 0.075, : arguments imply differing number of rows: 500, 599, 508, 489, 547, 624, 587, 534, 499, 494, 566, 520, 541, 543, 615"


I want my files to look like this (a file that combines all 100 text files of mine by the two columns):
File AB
A B
1 100
2 200
3 300
4 400
1 100
2 200
3 300
4 400
5 300
6 400
in one jumbo csv file. Please help me if this is possible. I'm new to scripting and will provide more info as necessary.

Upvotes: 1

Views: 1604

Answers (2)

IRTFM
IRTFM

Reputation: 263301

I think using dplyr or plyr package functions is really overkill. Suggest trying write.table, (since there are no commas in the desired output):

write.table(file_A, file="comb_file.txt")
write.table(file_B, file="comb_file.txt", append=TRUE)

You certainly could use write.csv but then the output would not look like what you illustrated.

Upvotes: 1

Manohar Swamynathan
Manohar Swamynathan

Reputation: 2095

Here is different options of achieving it.

R Code:

# Option 1: Using plyr
library(plyr)
datafiles <-list.files (pattern='*.txt') 
dataset <- ldply(datafiles, read.table, header=F)

# Option 2: Using fread
library(data.table)
datafiles <-list.files (pattern='*.txt') 
dataset = rbindlist(lapply( datafiles, fread, header=F))

# Option 3: do.call method
dataset <- do.call("rbind",lapply(datafiles,
                              FUN=function(files){read.table(files,
                                                             header=FALSE)}))

# Option 4: Loops are any time slow so avoid, but have put here just for reference
for (file in datafiles){  
  # if the merged dataset doesn't exist, create it
  if (!exists("dataset")){
    dataset <- read.table(file, header=FALSE)
  }

  # if the merged dataset does exist, append to it
  if (exists("dataset")){
    temp_dataset <-read.table(file, header=FALSE)
    dataset<-rbind(dataset, temp_dataset)
    rm(temp_dataset)
  }  
}

# Writing to csv
write.csv (dataset, ("file.csv"), row.names = FALSE, na="") 

Upvotes: 1

Related Questions