Skip to content

Merge the data from multiple text files

3 messages · Priya Arasu, David Winsemius, David L Carlson

#
I have multiple text files, where each file has Boolean rules.
Example of my text file 1 and 2
Text file 1:
A = not(B or C)
B = A and C
C = D
Text file 2:
A = D and E
B = not(D)

I want to merge the contents in text file as follows
A = not(B or C) and (D and E)
B = not(D) and (A and C)
C = D
Is there a code in R to merge the data from multiple text files?
Thank you
Priya?
#
On 1/5/19 7:28 AM, Priya Arasu via R-help wrote:
There is a `merge` function. For this use case you would need to first 
parse your expressions so that the LHS was in one character column and 
the RHS was in another character column in each of 2 dataframes. Then 
merge on the LHS columns and `paste` matching values from the two 
columns. You will probably need to learn how to use `ifelse` and `is.na`.
You also need to learn that R is a plain text mailing list and that each 
mail client has its own method for building mail in plain text.
#
To expand on David W's answer, here is an approach to your example. If you have many text files, you would want to process them together rather than individually. You gave us two examples so I'll use those and read them from the console using readLines(), but you would use the same function to open the files on your computer:
A = not(B or C)
B = A and C
C = D
A = D and E
B = not(D)
[1] "A = D and E"     "A = not(B or C)" "B = A and C"     "B = not(D)"
[5] "C = D"

Now we have combined the files into a single character vector called TF and sorted them. Next we need to parse them into the left and right hand sides. We will replace " = " with "\t" (tab) to do that:
LHS RHS
1 A   D and E
2 A   not(B or C)
3 B   A and C
4 B   not(D)
5 C   D

TF.data is a data frame with two columns. The tricky part is to add surrounding parentheses to rows 1 and 3 to get your example output:
LHS RHS
1 A   (D and E)
2 A   not(B or C)
3 B   (A and C)
4 B   not(D)
5 C   D

The first three lines identify the rows that have the word "and" but do not already have parentheses. The fourth line adds the surrounding parentheses. Finally we will combine the rows that belong to the same LHS value with split and create a list:
$`A`
[1] "(D and E)"   "not(B or C)"

$B
[1] "(A and C)" "not(D)"   

$C
[1] "D"
[,1]                           
[1,] "A = (D and E) and not(B or C)"
[2,] "B = (A and C) and not(D)"
[3,] "C = D"
The text file "TF.output.txt" contains the three lines.

----------------------------------------------
David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius
Sent: Saturday, January 5, 2019 1:12 PM
To: Priya Arasu <galaxie2485 at yahoo.co.in>; r-help at r-project.org
Subject: Re: [R] Merge the data from multiple text files
On 1/5/19 7:28 AM, Priya Arasu via R-help wrote:
There is a `merge` function. For this use case you would need to first 
parse your expressions so that the LHS was in one character column and 
the RHS was in another character column in each of 2 dataframes. Then 
merge on the LHS columns and `paste` matching values from the two 
columns. You will probably need to learn how to use `ifelse` and `is.na`.
You also need to learn that R is a plain text mailing list and that each 
mail client has its own method for building mail in plain text.