[R] Tables and merge

David Winsemius dwinsemius at comcast.net
Tue Jul 5 18:05:36 CEST 2011

On Jul 6, 2011, at 7:36 AM, Silvano wrote:

> ----- Original Message ----- From: "Silvano" <silvano at uel.br>
> To: <r-help at r-project.org>
> Sent: Thursday, June 30, 2011 9:07 AM
> Subject: Tables and merge
>> I have 21 files which is common variable CODE.
>> Each file refers to a question.
>> I would like to join the 21 files into one, to construct
>> tables for each question by CODE.
>> I tried the command (8 files only):
>> require(foreign)
>> q1 = read.epiinfo('Dados/Q1.rec')
>> q2 = read.epiinfo('Dados/Q2.rec')
>> q3 = read.epiinfo('Dados/Q3.rec')
>> q4 = read.epiinfo('Dados/Q4.rec')
>> q5 = read.epiinfo('Dados/Q5.rec')
>> q6 = read.epiinfo('Dados/Q6.rec')
>> q7 = read.epiinfo('Dados/Q7.rec')
>> q8 = read.epiinfo('Dados/Q8.rec')
>> juntos = merge(q1,q2,q3,q4,q5,q6,q7,q8)
>> But it didn't work. Any suggestions?

Suggestion # 1: Read the Posting Guide. In there you are advised to  
report the verbatim text from error messages. Reading error messages  
is often informative.

Suggestion # 2: Report the results of `str` on all of those "q"  
objects. We need to see whether there are the necessary common column  
names that would support a merge operation.

Suggestion #3 : read the ?merge page and pay particular attention to  
number ("two") in the title.

consider this possibility after further reading of ?merge and a bit of  

merge(x=q1, y=list(q2,q3,q4,q5,q6,q7,q8) )

# Your error occurred because of positional matching. The q3 object is  
being assigned to the third argument , "by= ", and that is what your  
unreported error message was telling you.

That construction seemed to work without error on a test I did with a  
slight modification of the first example on ?merge. After making the  
author column have the same name = `name`, I also got success with:

do.call("merge", list(x=authors, y=list(books, books)))

The non-do,call simplification above was not entirely predictably  
correct (to me anyway) , since the ?merge page does not say that a  
list object holding dataframes would be an acceptable "y" argument.  
But I see that as.data.frame(list(books, books)) does produce a  
data.frame and coercion with as.data.frame on that list object is  
probably what happened in the merge() call.

David Winsemius, MD
West Hartford, CT

More information about the R-help mailing list