2017-11-30 7 views
0

複数のファイルを1つのファイルにバインドしようとしていますが、個々のファイルの行数に戻るリストを生成しようとしています。これらのファイルを1つにバインドできますが、このリストの生成に失敗しました。誰か助けてくれますか?前もって感謝します。詳細/議論についてファイルを1つにバインドしてタグを生成する

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 

答えて

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) 

How to make a list of data framesを見ています。

+0

コードは非常にきちんと役立ちます。どうもありがとうございました! –

関連する問題