[R] Why do the results of paste() depend on how the argument

Michael Lachmann lachmann at eva.mpg.de
Mon Aug 2 18:15:00 CEST 2010



Erik Iverson-3 wrote:
> 
> 
> 
> Michael Lachmann wrote:
>>> On 08/01/2010 08:48 PM, thmsfuller066 at gmail.com wrote:
>>>> Hi,
>>>>
>>>> The following two 'df's should be the same, although their
>>>> constructions are different.
>>> But they aren't the same.
>>>
>>> df1 <- data.frame(X=c(1, 2, 3), Y=c(4, 5, 6))
>>> df2 <- data.frame(X=1:3, Y=4:6)
>>> identical(df1, df2)
>>>
>>> yields FALSE
>> 
>> 
>> Hmmm. quite strange.
>> 
> 
> *not* strange, in my opinion.
> 
> 
I'm sorry, but I worked on R for many years, and on other languages for
longer, and this is strange to me, and an easy mistake for someone to make.
But, it depends on your taste, I guess. In the end, it seems the problem is
with '4' defaulting to numeric, and 1:4 defaulting to integer.


Erik Iverson-3 wrote:
> 
>>> str( seq(1,3,by=1))
>>  num [1:3] 1 2 3
>> 
>>> str( seq.int(1,3,by=1))
>>  num [1:3] 1 2 3
>> 
>> I don't quite understand why this latest is num, and not int.
> 
> Just read ?seq
> 
>       Currently, ‘seq.int’ and the default method of ‘seq’ return a
>       result of type ‘"integer"’ (if it is representable as that and) if
>       ‘from’ is (numerically equal to an) integer and, e.g., only ‘to’
>       is specified, or also if only ‘length’ or only ‘along.with’ is
>       specified.  *Note:* this may change in the future and programmers
>       should not rely on it.
> 
If you read further down in my post, I did quote exactly this section of the
help file. Though in my version of the documentation, freshly installed a
few days ago, it only states:

seq.int and the default method of seq return a vector of type "integer" or
"double": programmers should not rely on which.

Your version does explain things better. But it doesn't make things less
confusing with respect to the original question. At least not for me.

But, what I meant to say above is that from the name of the function, I
expected seq.int to return int, and seq not neccessarily, and I was wrong. I
still don't exactly understand the different between seq and seq.int.

Erik Iverson-3 wrote:
> 
> 
>> Finally, a way to generate the same as 1:3! The help states: "seq_along
>> and
>> seq_length always return an integer vector."
>> 
>>> str( seq.int( 1L, 3L, by=1L ) )
>>  int [1:3] 1 2 3
>> 
>> You have to have all three, from to and by,  as integers if you want an
>> integer vector, I think.
> 
> No. See above.
> 

I meant to say: if you specify from, to and by, then all three have to be
integers for the result to be an integer. But you are right, seq(1, 3) gives
the same result as 1:3, and seq(1,3,by=1) does not.

-- 
View this message in context: http://r.789695.n4.nabble.com/Why-do-the-results-of-paste-depend-on-how-the-argument-data-frame-is-constructed-tp2309851p2310495.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list