1

for reshaping an array I use array_reshape() from reticulate package. For some purposes, I need a pre-allocated array consisting only of NA. Now let's see what array_reshape() does.

data <- c(NA, NA, NA)
a <- as.array(data)
a <- reticulate::array_reshape(a, dim = c(1L, 1L, 3L), order = "F")

The result of a is:

logi[1, 1, 1:3] TRUE TRUE TRUE

To get a comparison for this column-major ordering style (Fortran-style) using dim() leads to a different result

data <- c(NA, NA, NA)
a <- as.array(data)
dim(a) <- c(1L, 1L, 3L)

The result of a is how expected:

logi[1, 1, 1:3] NA NA NA

After further tests I found out that NA is transferred to TRUE if there is no numeric or alphanumeric (e.g. character) value in the array. If a e.g. numeric value is in the array all worked as expected

data <- c(NA, NA, 3)
a <- as.array(data)
a <- reticulate::array_reshape(a, dim = c(1L, 1L, 3L), order = "F")
logi[1, 1, 1:3] NA NA 3

But, if using logical values in the array too, the result will be

data <- c(NA, NA, FALSE)
a <- as.array(data)
a <- reticulate::array_reshape(a, dim = c(1L, 1L, 3L), order = "F")
logi[1, 1, 1:3] TRUE TRUE FALSE

That's in my opinion very "dangerous" because later no distinction can be made whether the original value is NA or logical TRUE. Using dim() always leads to results as expected.

PS: I know that internally a NA is treated as a logical constant of length 1.

Do you have any hints or suggestions to solve this reshaping issue with array_reshape()?

Thanks in advance

stschn
  • 121
  • 7
  • This may be relevant https://github.com/rstudio/reticulate/issues/197 and `data <- c(NA_real_, NA_real_, NA_real_)` gives expected, but then `data <- c(NA_real_, NA_real_, FALSE)` converts the `FALSE` to `0`, and `data <- c(NA_integer_, NA_integer_, NA_integer_)` is not great. – user20650 Aug 23 '20 at 16:15
  • Thanks for your answer and the link you provided. – stschn Aug 26 '20 at 17:37

0 Answers0