[R] c(), rbind and cbind functions - why type of resulting object is double

Patrick Burns pburns at pburns.seanet.com
Tue Jan 22 19:08:12 CET 2013


One place that talks about what Bill says is:

http://www.burns-stat.com/documents/tutorials/impatient-r/more-r-key-objects/more-r-numbers/

Pat

On 22/01/2013 17:35, William Dunlap wrote:
>> I was wondering
>> why I don't get an integer vector or and integer matrix with the following
>> code:
>>> z <- c(1, 2:0, 3, 4:8)
>>> typeof(z)
>> [1] "double"
>
> It is because the literals 1 and 3 have type "double".  Append "L" to make
> them literal integers.
>    > typeof(c(1L, 2:0, 3L, 4:8))
>    [1] "integer"
> The colon function (":") returns an integer vector if it can do so without giving
> a numerically incorrect answer.
>
>    > typeof(1.0:3.0)
>    [1] "integer"
>    > typeof(1.5:3.5)
>    [1] "double"
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
>> Of Lourdes Peña Castillo
>> Sent: Tuesday, January 22, 2013 9:26 AM
>> To: r-help at r-project.org
>> Subject: [R] c(), rbind and cbind functions - why type of resulting object is double
>>
>> Hello Everyone,
>>
>> I am using R 2.15.0 and I came across this behaviour and I was wondering
>> why I don't get an integer vector or and integer matrix with the following
>> code:
>>
>>> z <- c(1, 2:0, 3, 4:8)
>>
>>> typeof(z)
>>
>> [1] "double"
>>
>>> z <- rbind(1, 2:0, 3, 4:8)
>>
>> Warning message:
>>
>> In rbind(1, 2:0, 3, 4:8) :
>>
>>    number of columns of result is not a multiple of vector length (arg 2)
>>
>>> typeof(z)
>>
>> [1] "double"
>>
>>> z <- matrix(c(1, 2:0, 3, 4:8), nrow = 5)
>>
>>> typeof(z)
>>
>> [1] "double"
>>
>>
>> Shouldn't be typeof integer? According to the online help if everything is
>> integer the output should be integer.
>> But if I do this, I get an integer matrix.
>>
>>> z <- matrix(1:20, nrow = 5)
>>
>>> typeof(z)
>>
>> [1] "integer"
>>
>> Thanks!
>>
>> Lourdes
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>

-- 
Patrick Burns
pburns at pburns.seanet.com
twitter: @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Impatient R' and 'The R Inferno')



More information about the R-help mailing list