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

Erik Iverson eriki at ccbr.umn.edu
Mon Aug 2 17:47:27 CEST 2010



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.

>> str(1:3)
>  int [1:3] 1 2 3
> 
>> 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.

You're specifying 'by' in your calls.

Try

str(seq.int(1, 3))
str(seq(1, 3))


> 
>> str( seq_along( seq(1,3,by=1)) )
>  int [1:3] 1 2 3
> 
> 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.



More information about the R-help mailing list