Thanks Erin and Jim. You have indeed solved my problem. Philip Quoting Erin Hodgess <erinm.hodgess at gmail.com>:> Hi Philip: > > Here is something to consider: > >> #potential solution: >> sta <- paste(st,"-01",sep="") >> st1 <- as.Date(sta, format=("%Y-%m-%d")) >> print(st1) > [1] "1961-01-01" "1961-04-01" "1983-02-01" > > > Hope this helps! > Erin > > Erin Hodgess, PhD > mailto: erinm.hodgess at gmail.com > > > On Sun, Aug 19, 2018 at 3:25 PM <philipsm at cpanel1.stormweb.net> wrote: > >> I am having trouble with what must be a very simple problem. Here is a >> reproducible example: >> >> library(lubridate) >> st <- c("1961-01","1961-04","1983-02") >> print(st) >> #[1] "1961-01" "1961-04" "1983-02" >> st1 <- as.Date(st, format=("%Y-%m")) >> print(st1) >> #[1] NA NA NA >> >> Why the heck am I getting three NAs instead of three Dates?I have >> studied the R documentation for as.Date() and it has not turned on the >> light bulb for me. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >>
You loaded "lubridate" so using Erin's approach library(lubridate) st <- c("1961-01","1961-04","1983-02") dat1 <- ymd(paste( st, "01",? sep ="-")) On Monday, August 20, 2018, 1:15:56 a.m. EDT, <philipsm at cpanel1.stormweb.net> wrote: Thanks Erin and Jim. You have indeed solved my problem. Philip Quoting Erin Hodgess <erinm.hodgess at gmail.com>:> Hi Philip: > > Here is something to consider: > >> #potential solution: >> sta <- paste(st,"-01",sep="") >> st1 <- as.Date(sta, format=("%Y-%m-%d")) >> print(st1) > [1] "1961-01-01" "1961-04-01" "1983-02-01" > > > Hope this helps! > Erin > > Erin Hodgess, PhD > mailto: erinm.hodgess at gmail.com > > > On Sun, Aug 19, 2018 at 3:25 PM <philipsm at cpanel1.stormweb.net> wrote: > >> I am having trouble with what must be a very simple problem. Here is a >> reproducible example: >> >> library(lubridate) >> st <- c("1961-01","1961-04","1983-02") >> print(st) >> #[1] "1961-01" "1961-04" "1983-02" >> st1 <- as.Date(st, format=("%Y-%m")) >> print(st1) >> #[1] NA NA NA >> >> Why the heck am I getting three NAs instead of three Dates?I have >> studied the R documentation for as.Date() and it has not turned on the >> light bulb for me. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >>______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]]
Nice one! On Tue, Aug 21, 2018 at 6:14 PM John Kane <jrkrideau at yahoo.ca> wrote:> You loaded "lubridate" so using Erin's approach > > library(lubridate) > st <- c("1961-01","1961-04","1983-02") > dat1 <- ymd(paste( st, "01", sep ="-")) > > > On Monday, August 20, 2018, 1:15:56 a.m. EDT, < > philipsm at cpanel1.stormweb.net> wrote: > > > Thanks Erin and Jim. You have indeed solved my problem. > > Philip > > > Quoting Erin Hodgess <erinm.hodgess at gmail.com>: > > > Hi Philip: > > > > Here is something to consider: > > > >> #potential solution: > >> sta <- paste(st,"-01",sep="") > >> st1 <- as.Date(sta, format=("%Y-%m-%d")) > >> print(st1) > > [1] "1961-01-01" "1961-04-01" "1983-02-01" > > > > > > Hope this helps! > > Erin > > > > Erin Hodgess, PhD > > mailto: erinm.hodgess at gmail.com > > > > > > On Sun, Aug 19, 2018 at 3:25 PM <philipsm at cpanel1.stormweb.net> wrote: > > > >> I am having trouble with what must be a very simple problem. Here is a > >> reproducible example: > >> > >> library(lubridate) > >> st <- c("1961-01","1961-04","1983-02") > >> print(st) > >> #[1] "1961-01" "1961-04" "1983-02" > >> st1 <- as.Date(st, format=("%Y-%m")) > >> print(st1) > >> #[1] NA NA NA > >> > >> Why the heck am I getting three NAs instead of three Dates?I have > >> studied the R documentation for as.Date() and it has not turned on the > >> light bulb for me. > >> > >> ______________________________________________ > >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> 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. > >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Erin Hodgess, PhD mailto: erinm.hodgess at gmail.com [[alternative HTML version deleted]]