I have a folder called simulations
that contains 100 sub-folders, each of which contains the results of simulations. The results of each simulation in each sub-folder are in four separate files named, seq[1].nex
, seq[2].nex
, seq[3].nex
, and seq[4].nex
. Each of these files has the same format, which is as follows:
#NEXUS
Begin data;
Dimensions ntax=5 nchar=55;
Format datatype=Standard symbols="01" missing=? gap=-;
Matrix
L1 1100110010010100010110000110000010000100001011010010110
L2 1101110110011010010000010111000010010000001001010110110
L3 0111111100010100010011000001100011010100010010110011110
L4 1101110110011010010000010111000010010000001001010110110
L5 1101110100110100010110010110001010010100001011010110100
;
End;
The files named seq
have the same number of rows (i.e., L1-L5), but they differ in the length of each row. For instance, seq[2].nex
looks as follows:
#NEXUS
Begin data;
Dimensions ntax=5 nchar=20;
Format datatype=Standard symbols="012" missing=? gap=-;
Matrix
L1 10000012202011210001
L2 10002112212010210012
L3 10002112212210220022
L4 10002112212010220012
L5 10001112212010222012
;
End;
For each of the 100 sub-folders, I want to merge seq[1].nex
, seq[2].nex
, seq[3].nex
, and seq[4].nex
into one file seq.nex
. Starting with seq[1].nex
, I want to append the information from the later files (i.e., 2-4) to its corresponding row in the first file. Using the two examples above, the output that I want would look like this:
#NEXUS
Begin data;
Dimensions ntax=5 nchar=55;
Format datatype=Standard symbols="01" missing=? gap=-;
Matrix
L1 110011001001010001011000011000001000010000101101001011010000012202011210001
L2 110111011001101001000001011100001001000000100101011011010002112212010210012
L3 011111110001010001001100000110001101010001001011001111010002112212210220022
L4 110111011001101001000001011100001001000000100101011011010002112212010220012
L5 110111010011010001011001011000101001010000101101011010010001112212010222012
;
End;
I then want to repeat this process of merging the file for each of the 100 sub-folder. Is there a way to do this in R?