1

This is very similar to this question: sprintf invalid format '%d', but it is different. sprintf() gives an error formatting to "%d" for c(NA_integer_, 1), but not c(1, NA_integer).

sprintf("%d", NA)
#> [1] "NA"
sprintf("%d", 1)
#> [1] "1"
sprintf("%d", NA_integer_)
#> [1] "NA"
sprintf("%d", c(1, NA_integer_))
#> [1] "1"  "NA"
sprintf("%d", c(NA_integer_, 1))
#> Error in sprintf("%d", c(NA_integer_, 1)) : 
#>   invalid format '%d'; use format %f, %e, %g or %a for numeric objects

Where it differs from the question above is that sprintf("%d", NA_integer_) works fine. Also, NA_integer_ is explicitly an integer, so surely that can't be the problem?

Oliver
  • 1,098
  • 1
  • 11
  • 16
  • 5
    When you combine that it is going to `numeric`, try `sprintf("%d", c(NA_integer_, 1L))` – akrun Mar 16 '18 at 14:12
  • Oh interesting. But why do you then not have the problem with `sprintf("%d", c(1L, 1))`? – Oliver Mar 16 '18 at 14:19
  • It may be because of the type conversion to a single one i.e. `class(c(1L, 1))# [1] "numeric"`. In the former case, you are specifically asking `NA_integer_` and coerce with `1` i.e. `class(c(NA_integer_, 1))# [1] "numeric"` and there could be some clashes – akrun Mar 16 '18 at 14:20

0 Answers0