From ripley at stats.ox.ac.uk  Tue May 16 23:38:46 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 16 May 2006 22:38:46 +0100 (BST)
Subject: [Rd] bug in rbind.data.frame with factors (PR#8868)
In-Reply-To: <446A4325.3080609@math.ucalgary.ca>
References: <20060516195031.28119494DB@slim.kubism.ku.dk>
	<446A4325.3080609@math.ucalgary.ca>
Message-ID: <Pine.LNX.4.64.0605162228370.11772@gannet.stats.ox.ac.uk>

On Tue, 16 May 2006, Peter Ehlers wrote:

> How is this a bug? From the help page for cbind/rbind:
>
> Description
> Take a sequence of vector, matrix or data frames arguments and
> combine by _columns_ or _rows_, respectively.
> (emphasis added)
>
> Note that it does _not_ say "combine by variable names".

That was my first reaction, but in fact it does attempt to match up the 
colunn names.  So this is a bug in an undocumented extension, the bug 
being that when it looks into extending the levels of a factor, it checks 
if the column number was a factor in the first data frame and not in the 
(possibly permuted) columns of the data frame under consideration.

It is unclear from the S documentation what it is supposed to do, but it 
seems that it generates the same problematic output.

>
> Peter Ehlers
>
> rafalku at gmail.com wrote:
>
>> Full_Name: Rafal Kustra
>> Version: 2.1.1

Please don't report on obselete versions of R: we do ask so quite 
explicitly.

>> OS: Linux, MacOS 10.3
>> Submission from: (NULL) (69.195.47.62)
>>
>>
>> When Rbinding two data frames with factors, strange result occur (but no error)
>> when the order of data frame variables is different in two data frames:
>>
>>
>>> d1=as.data.frame(list(x=1:10,y=letters[1:10]))
>>> d2=as.data.frame(list(y=LETTERS[1:5],x=7:11))
>>> d2
>>
>>   y  x
>> 1 A  7
>> 2 B  8
>> 3 C  9
>> 4 D 10
>> 5 E 11
>>
>>> rbind(d1,d2)
>>
>>     x    y
>> 1   1    a
>> 2   2    b
>> 3   3    c
>> 4   4    d
>> 5   5    e
>> 6   6    f
>> 7   7    g
>> 8   8    h
>> 9   9    i
>> 10 10    j
>> 11  7 <NA>
>> 21  8 <NA>
>> 31  9 <NA>
>> 41 10 <NA>
>> 51 11 <NA>
>> Warning message:
>> invalid factor level, NAs generated in: "[<-.factor"(`*tmp*`, ri, value = c("A",
>> "B", "C", "D", "E"))
>>
>>
>> Things work correctly when the order of variables is the same:
>>
>>
>>> d3=as.data.frame(list(x=7:11,y=LETTERS[1:5]))
>>> rbind(d1,d3)
>>
>>     x y
>> 1   1 a
>> 2   2 b
>> 3   3 c
>> 4   4 d
>> 5   5 e
>> 6   6 f
>> 7   7 g
>> 8   8 h
>> 9   9 i
>> 10 10 j
>> 11  7 A
>> 21  8 B
>> 31  9 C
>> 41 10 D
>> 51 11 E
>>
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] bug in rbind.data.frame with factors (PR#8868)
Prof Brian Ripley