[R] Need Help on Joining 2 text files in R

Prasenjit Kapat kapatp at gmail.com
Wed May 23 17:41:54 CEST 2007


On Wednesday 23 May 2007 07:31:47 am CHANDRAMOULI BANERJEE wrote:
> Hi,
>
>   I have  2 text files , the first one looks like this :
>
>   CC: " some statements"
>   this is followed by 10-12 lines in the similar pattern and then the next
> line is a comment and from the 14th line all the columns are aligned and
> the variable values are filled in ....
>
>   The next file is a text file with 3 columns with column names and the 2
> files have the same number of rows in both.....
>
>   I want to concatanate these 2 files side by side to make a complete file,
> keeping the structure of the first file same......
>
>   More explicitly the first file looks like:
>
>   CC:"....some statement............."
>   CC:....same as above.......
>
>   .............................this pattern is followed upto say 10-12
> lines and then some variables , I mean columns used in the file is listed
> as a string like nuclear_intensity; cell_intensity ;....and so on .......
>
>   and then in the next row onwards the column headers start with column
> names and then the variable values are plugged in .......I wish I could
> have sent you the file but it is in the intranet system and can't be sent
> over in email.......

If the file has read access, then it can always be copied and attached, unless 
you are prohibited from distributing the file in public domain.

>
>
>   The second file looks simple with column headers and field values.....The
> 2 files are both in the text format.....
>
>   What I really need to know is how to join the 2 text files and make it
> into one keeping the structure of the first one intact.......I need to join
> the columns with the column names from the second file to the first
> one.....
>
>   I will wait for any help of how to achieve this is R.

The cheapest way (if possible) is to use an editor that supports block 
selection (like, kate, emacs) then block select the 3 columns from the second 
file and paste them beside the last column in the first one. Done.

Now, if the merging has to be done in R, one way is to read.table the first 
file with 'skip=13' that way you will get only the data. Then read.table from 
the second file and cbind the two data.frames. Finally write.table into a new 
file. By this the data will merged but to add the comments, I think you will 
have to do it manually, copy from the first file and paste into the new file, 
using whatever text editor you use. (I assume the file is a text file)

HTH,
PK



More information about the R-help mailing list