From ecortens at mtroyal.ca  Tue May 23 16:53:34 2017
From: ecortens at mtroyal.ca (Evan Cortens)
Date: Tue, 23 May 2017 08:53:34 -0600
Subject: [Rd] Inconsistency in handling of numeric input with %d by
	sprintf
In-Reply-To: <CAPRVBcx3OjFK0G=hk1g8qChYiQ7cuZPpep-kju-_BD4MErhDuA@mail.gmail.com>
References: <CAPRVBcx3OjFK0G=hk1g8qChYiQ7cuZPpep-kju-_BD4MErhDuA@mail.gmail.com>
Message-ID: <CABKQe-bStQz+AOQ7SGMcdiNY9BNOuaz21j7b3Mtp6-tqtpdbPg@mail.gmail.com>

Hi Michael,

I posted something on this topic to R-devel several weeks ago, but never
got a response. My ultimate conclusion is that sprintf() isn't super
consistent in how it handles coercion: sometimes it'll coerce real to
integer without complaint, other times it won't. (My particular email had
to do with the vectors longer than 1 and their positioning vis-a-vis the
format string.) The safest thing is just to pass the right type. In this
case, sprintf('%d', as.integer(NA_real_)) works.

Best,

Evan

On Fri, May 19, 2017 at 9:23 AM, Michael Chirico <michaelchirico4 at gmail.com>
wrote:

> Consider
>
> #as.numeric for emphasis
> sprintf('%d', as.numeric(1))
> # [1] "1"
>
> vs.
>
> sprintf('%d', NA_real_)
>
> >  Error in sprintf("%d", NA_real_) :
>
>    invalid format '%d'; use format %f, %e, %g or %a for numeric object
> >
>
> I understand the error is correct, but if it works for other numeric input,
> why doesn't R just coerce NA_real_ to NA_integer_?
>
> Michael Chirico
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Evan Cortens, PhD
Institutional Analyst - Office of Institutional Analysis
Mount Royal University
403-440-6529

	[[alternative HTML version deleted]]



[Rd] Inconsistency in handling of numeric input with %d by sprintf
Evan Cortens