[R] strange Sys.Date() side effect

Czerminski, Ryszard Ryszard.Czerminski at astrazeneca.com
Fri Jan 13 15:16:34 CET 2012


Basically I want to create a string vector where first element is a
date.

>> Note that if you supply c() with objects of different types (as you
have),
>> the results will probably not be what you wanted.

This is the key. I assumed (not correctly)
that Sys.Date() generates a character

> Sys.Date()
[1] "2012-01-13"

but as you point out:

> class(Sys.Date())
[1] "Date"
  
and it is the best to provide c() with arguments of the same type
so, in the end, explicitely casting Sys.Date() to character works for
me:

> N <- 2; c(as.character(Sys.Date()), sprintf('N=%d', N))
[1] "2012-01-13" "N=2"

Thanks!

Ryszard


--------------------------------------------------------------------------
Confidentiality Notice: This message is private and may contain confidential and proprietary information. If you have received this message in error, please notify us and remove it from your system and note that you must not copy, distribute or take any action in reliance on it. Any unauthorized use or disclosure of the contents of this message is not permitted and may be unlawful.
 
-----Original Message-----
From: MacQueen, Don [mailto:macqueen1 at llnl.gov] 
Sent: Thursday, January 12, 2012 5:27 PM
To: Czerminski, Ryszard; r-help at r-project.org
Subject: Re: [R] strange Sys.Date() side effect

My best guess is that you are misunderstanding what the c() function
does.
I'd suggest reading the help page for c, obtained by typing
  ?c
Note that if you supply c() with objects of different types (as you
have),
the results will probably not be what you wanted.

Given what c() does, your output and warning message make sense. But I'm
unable to figure out what you're really trying to do. Something like
this,
perhaps?

> N <- 2; sprintf('N = %d', N)
[1] "N = 2"


-Don


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 1/10/12 5:04 AM, "Czerminski, Ryszard"
<Ryszard.Czerminski at astrazeneca.com> wrote:

>Any ideas what is the problem with this code?
>
>> N <- 2; c(Sys.Date(), sprintf('N = %d', N))
>[1] "2012-01-10" NA
>Warning message:
>In as.POSIXlt.Date(x) : NAs introduced by coercion
>
>Best regards,
>Ryszard
>
>Ryszard Czerminski
>AstraZeneca Pharmaceuticals LP
>35 Gatehouse Drive
>Waltham, MA 02451
>USA
>781-839-4304
>ryszard.czerminski at astrazeneca.com
>
>
>-----------------------------------------------------------------------
---
>Confidentiality Notice: This message is private and may
...{{dropped:11}}
>
>______________________________________________
>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