[R] Why do the results of paste() depend on how the argument
(Ted Harding)
Ted.Harding at manchester.ac.uk
Mon Aug 2 11:15:03 CEST 2010
It may throw light on the confusion to get right down to basics:
str(c(1,2,3))
# num [1:3] 1 2 3
str(1:3)
# int [1:3] 1 2 3
typeof(c(1,2,3))
# [1] "double"
typeof(1:3)
# [1] "integer"
so, to start with, they are simply not variables of the same type
(real versus integer). However, once you start to use them, one
is likely to be coerced into the same type as the other if they
occur together in an expression. Hence the "confusing" results
from Bill Venables!
Ted.
On 02-Aug-10 04:09:35, Bill.Venables at csiro.au wrote:
> Just to add to the confusion, for (nearly) all practical purposes they
> are the same:
>
>> all.equal(df1, df2)
> [1] TRUE
>> do.call(paste, df1)
> [1] "1 4" "2 5" "3 6"
>> do.call(paste, df2)
> [1] "1 4" "2 5" "3 6"
>>
>
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Erik Iverson
> Sent: Monday, 2 August 2010 1:48 PM
> To: thmsfuller066 at gmail.com
> Cc: r-help at r-project.org
> Subject: Re: [R] Why do the results of paste() depend on how the
> argument (data.frame) is constructed?
>
> 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
>
> See
>
> str(df1)
> str(df2)
>
> to see how they differ.
>
>
>
> But the results of paste() are different.
>> I don't see this is explained in ?paste. Could you help me understand
>> why it is like this?
>>
>>> df=data.frame(X=c(1, 2, 3), Y=c(4, 5, 6))
>>> df
>> X Y
>> 1 1 4
>> 2 2 5
>> 3 3 6
>>> paste(df)
>> [1] "c(1, 2, 3)" "c(4, 5, 6)"
>>> df=data.frame(X=1:3, Y=4:6)
>>> df
>> X Y
>> 1 1 4
>> 2 2 5
>> 3 3 6
>>> paste(df)
>> [1] "1:3" "4:6"
>>>
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 02-Aug-10 Time: 10:14:59
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list