Search code examples
rtype-conversionintegerdouble

How to convert from the "integer" to the "double" object type while retaining columns in R?


I want my data to be the "double" rather than the "integer" object category to be able to work with a specific R package, while retaining the separation of the data into distinct columns designating different sites. I have tried using the as.double() function, but this creates a string of numbers which are not separated into the columns which they have previously. Is there a way to convert to doubles to be able to work with this R package while retaining the categorisation of the data in its currents rows & columns?

To give you an example of the data I am trying to convert, and what I am trying to convert it to, here is one of the integers I have in my data:

structure(c(97L, 97L, 98L, 98L, 98L, 102L, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA), dim = c(6L, 3L), dimnames = list(
    NULL, c("det.covs.day.day141", "det.covs.day.day142", "det.covs.day.day143"
    )))

Here is a subset of that data:

      det.covs.day.day141 det.covs.day.day142 det.covs.day.day143
 [1,]                  97                  NA                  NA
 [2,]                  97                  NA                  NA
 [3,]                  98                  NA                  NA
 [4,]                  98                  NA                  NA
 [5,]                  98                  NA                  NA
 [6,]                 102                  NA                  NA
 [7,]                 103                  NA                  NA
 [8,]                 103                  NA                  NA
 [9,]                 108                  NA                  NA
[10,]                 124                  NA                  NA
[11,]                 126                 127                 128
[12,]                 136                 137                 138
[13,]                 148                 148                 149
[14,]                 188                  NA                  NA
[15,]                 204                 210                  NA
[16,]                 204                 210                  NA
[17,]                 204                 210                  NA
[18,]                 204                 210                  NA
[19,]                 204                 210                  NA
[20,]                 204                 210                  NA
[21,]                 204                 210                  NA
[22,]                 204                 210                  NA
[23,]                 204                 210                  NA
[24,]                 204                 210                  NA
[25,]                 204                 210                  NA
[26,]                 204                 210                  NA
[27,]                 204                 210                  NA
[28,]                 204                 210                  NA
[29,]                 204                 210                  NA
[30,]                 205                  NA                  NA

And here is the equivalent part of data in the tutorial for the R package:

structure(c(156, 156, 156, 156, 156, 156, 167, 167, 167, 167, 
167, 167, 187, 187, 187, 187, 187, 187), dim = c(6L, 3L))

And here is a subset of the data used in the tutorial:

       [,1] [,2] [,3]
  [1,]  156  167  187
  [2,]  156  167  187
  [3,]  156  167  187
  [4,]  156  167  187
  [5,]  156  167  187
  [6,]  156  167  187
  [7,]  156  167  180
  [8,]  156  167  180
  [9,]  156  167  180
 [10,]  156  167  180
 [11,]  156  167  180
 [12,]  156  167  180
 [13,]  156  167  180
 [14,]  156  167  180
 [15,]  156  167  180
 [16,]  156  167  181
 [17,]  156  167  181
 [18,]  156  167  181
 [19,]  156  167  180
 [20,]  156  167  180
 [21,]  156  167  180
 [22,]  156  167  180
 [23,]  156  167  180
 [24,]  156  167  180
 [25,]  156  167  180
 [26,]  156  167  180
 [27,]  156  167  187
 [28,]  156  167  187
 [29,]  156  167  187
 [30,]  156  167  187

In case it helps, the R package in question is spOccupancy https://cran.r-project.org/web/packages/spOccupancy/index.html


Solution

  • The two functions as.double() and as.numeric() are identical and they both convert a matrix into a vector, as you have noticed.

    The way to go is to use apply() to apply the function as.numeric() (or as.double(), if you prefer) to each column of the matrix as follows:

    m <- structure(c(97L, 97L, 98L, 98L, 98L, 102L, NA, NA, NA, NA, NA,
      NA, NA, NA, NA, NA, NA, NA), dim = c(6L, 3L), dimnames = list(
      NULL, c("det.covs.day.day141", "det.covs.day.day142", "det.covs.day.day143"
      )))
    m_num <- apply(m, 2, as.numeric)
    m_num
    ##      det.covs.day.day141 det.covs.day.day142 det.covs.day.day143
    ## [1,]                  97                  NA                  NA
    ## [2,]                  97                  NA                  NA
    ## [3,]                  98                  NA                  NA
    ## [4,]                  98                  NA                  NA
    ## [5,]                  98                  NA                  NA
    ## [6,]                 102                  NA                  NA
    

    As you can see, the shape of the matrix and the column names are kept intact. The new matrix is no longer an integer:

    is.integer(m_num)
    ## [1] FALSE
    

    It is important to note, that integers are also numeric:

    is.numeric(m)
    ## [1] TRUE
    is.numeric(m_num)
    ## [1] TRUE
    

    So, you can not use is.numeric() to distinguish between integers and doubles, but is.double() can do the job:

    is.double(m)
    ## [1] FALSE
    is.double(m_num)
    ## [1] TRUE
    

    Somewhat confusingly, floating point numbers are nevertheless called "numeric" in R:

    class(2.5)
    ## [1] "numeric"