[R] Bug in "transform"?

hadley wickham h.wickham at gmail.com
Wed Dec 3 14:20:55 CET 2008


On Wed, Dec 3, 2008 at 2:06 AM, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Tue, 2 Dec 2008, hadley wickham wrote:
>
>>> The underlying issue is actually not in transform() but in data.frame():
>>>
>>>> aq <- airquality[sample(1:153,6),]
>>>> data.frame(aq, list(a=1,b=2))
>>>
>>> Error in data.frame(aq, list(a = 1, b = 2)) :
>>>  arguments imply differing number of rows: 6, 1
>>>>
>>>> data.frame(aq, list(a=1))
>>>
>>>   Ozone Solar.R Wind Temp Month Day a
>>> 3      12     149 12.6   74     5   3 1
>>> 31     37     279  7.4   76     5  31 1
>>> 34     NA     242 16.1   67     6   3 1
>>> 65     NA     101 10.9   84     7   4 1
>>> 59     NA      98 11.5   80     6  28 1
>>> 133    24     259  9.7   73     9  10 1
>>>
>>
>> Is this a bug or a "feature"?
>
> As documented:
>
>  Objects passed to data.frame should have the same number of rows, but
>  atomic vectors, factors and character vectors protected by I will be
>  recycled a whole number of times if necessary.
>
> How did you manage to miss that in the help page?

Because it's not true?

# These work:
data.frame(data.frame(1:10), data.frame(1))
data.frame(data.frame(1:10), data.frame(1), data.frame(5))
data.frame(data.frame(1:10, 1), data.frame(5))

# This doesn't
data.frame(data.frame(1:10), data.frame(1, 5))

Clearly there are situations in which data frames _are_ recycled.

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list