-1

I am trying to bind several files into a single one and also generate a list which returns to the number of rows for each individual file. I can bind those files into one, but I failed to generate this list. Could anyone help me? Thanks in advance.

aa = c(1:3) #files with .1 or .2 suffix
bb = paste0(datasource,"file.",aa) # paste all those files into one 
merge.data = read.table(bb[1]) # read the first file as a data.frame
n = rnow(merge.data) # this will be the number of row under the first file 

for (i in 2:n){
  new.data = read.table(bb[i]) # read all following files
  merge.data = rbind(merge.data, new.data) # bind those files into a single one 
}
merge.data # this the whole data.frame contains all original files' data

yy = paste0(datasource,"file.",aa)

merge.data = data.frame()
##### I would like to add another variable np, which is a list representing the number of rows for each individual file before merge
np = NULL
for (i in 1:n){
  merge.data[i] = read.table(bb[i])
  np[i] = nrow(merge.data[i])
  merge.data = rbind(merge.data[i])
}
np
merge.data
Amy_777
  • 77
  • 2
  • 13

1 Answers1

1
aa = c(1:3) #files with .1 or .2 suffix
bb = paste0(datasource,"file.",aa) # paste all those files into one 

# read all files into list of data frames
bb_list = sapply(bb, read.table, simplify = F)
# get vector of number of rows
np = sapply(bb_list, nrow)
# combine list of data frames into one
result = do.call(rbind, bb_list)

For more details/discussion, have a look at How to make a list of data frames.

Gregor Thomas
  • 104,719
  • 16
  • 140
  • 257