[R] c(), rbind and cbind functions - why type of resulting object is double
William Dunlap
wdunlap at tibco.com
Tue Jan 22 18:35:57 CET 2013
> 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.
More information about the R-help
mailing list