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

Michael Lachmann lachmann at eva.mpg.de
Mon Aug 2 17:39:22 CEST 2010


> 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.

> 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.

> 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.

> str( seq( 1L, 3L , by=1L) )
 int [1:3] 1 2 3

So what is the difference between seq and seq.int? Maybe that seq.int is
faster....

Of course, there is the easy way:
> str( as.integer(seq(1,3,by=1)))
 int [1:3] 1 2 3

It seems to be all included in the small cryptic comment in the help file:
"seq.int and the default method of seq return a vector of type "integer" or
"double": programmers should not rely on which."

Finally,
> as.character( list(1:3))
1] "1:3"

> as.character( list( c(1:3,4)))
[1] "c(1, 2, 3, 4)"

> as.character( list( c(1:3,4L)))
[1] "1:4"

I guess I have to be really careful to add "L" at the end when talking about
integers. This raises the question why '4' defaults to double, whereas '1:4'
defaults to integer. After all 1.5:4.5 is possible, so sequences aren't
always integer.

So, finally, to get back to the original question:

> On 08/01/2010 08:48 PM, thmsfuller066 at gmail.com wrote:
> 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)
> FALSE

Do this:
> df3 <- data.frame(X=c(1L, 2L, 3L), Y=c(4L, 5L, 6L))
> identical( df3, df2)
[1] TRUE

Just add 'L' at the end when talking about integers.
You learn something new every day.

Michael

P.S. I don't think there is a 'short' way to produce a double/numeric 1:3. I
think you have to use
as.numeric(1:3). Or 1:3+0.0.


-- 
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-tp2309851p2310441.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list