Re: [R] extracting months from a data

2016-03-10 Thread KMNanus
Thanks for this. I wasn’t familiar with paste0, so I’ll call that and see if it works. Ken kmna...@gmail.com 914-450-0816 (tel) 347-730-4813 (fax) > On Mar 9, 2016, at 7:15 PM, Dalthorp, Daniel wrote: > > Or: > > x <- c( "3-Oct", "10-Nov" ) > format(as.Date(paste0(x,rep("-1970",length(x))

Re: [R] extracting months from a data

2016-03-09 Thread William Dunlap via R-help
How much do you care about dealing with misformatted date strings, like "111-Oct" or "12-Mai"? Flagging those may be more important than milliseconds of CPU time. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Mar 9, 2016 at 5:24 PM, Dalthorp, Daniel wrote: > How about the following, whi

Re: [R] extracting months from a data

2016-03-09 Thread Dalthorp, Daniel
How about the following, which is both faster and simpler than either the as.Date(...) or sub(...) solutions discussed earlier: substring(x,first=nchar(x)-2) require(microbenchmark) microbenchmark(format(as.Date(paste0(x,"-2016"),format='%d-%b-%Y'),'%b')) # 59.3 microseconds on my computer microb

Re: [R] extracting months from a data

2016-03-09 Thread Jeff Newmiller
How about slower? That is objective. I use dates all the time so I am quite familiar with what they are good for. However, I prefer to avoid inventing information such as which year the date should have included unless I have to based on knowledge of the data source. It is not good to mislead

Re: [R] extracting months from a data

2016-03-09 Thread Dalthorp, Daniel
Good point about 29-Feb...fixed in the following: format(as.Date(paste0(x,"-2016"),format='%d-%b-%Y'),'%b') # Also: The date functions can be used to easily calculate passage of time and offer good flexibility for formatting output. -Dan P.S. "harder to understand" is in the eye of the beholder

Re: [R] extracting months from a data

2016-03-09 Thread Jeff Newmiller
Still not recommended. That takes more steps, is harder to understand, and will break when given "29-Feb" as input. -- Sent from my phone. Please excuse my brevity. On March 9, 2016 4:15:31 PM PST, "Dalthorp, Daniel" wrote: >Or: > >x <- c( "3-Oct", "10-Nov" ) >format(as.Date(paste0(x,rep("-19

Re: [R] extracting months from a data

2016-03-09 Thread Dalthorp, Daniel
Or: x <- c( "3-Oct", "10-Nov" ) format(as.Date(paste0(x,rep("-1970",length(x))),format='%d-%b-%Y'),'%b') # the 'paste0' appends a year to the text vector # the 'as.Date' interprets the strings as dates with format 10-Jun-2016 (e.g.) # the 'format' returns a string with date in format '%b' (which

Re: [R] extracting months from a data

2016-03-09 Thread Dalthorp, Daniel
?as.Date On Wed, Mar 9, 2016 at 10:14 AM, KMNanus wrote: > I have a series of dates in format 3-Oct, 10-Oct, 20-Oct, etc. > > I want to create a variable of just the month. If I convert the date to a > character string, substr is ineffective because some of the dates have 5 > characters (3-Oct

Re: [R] extracting months from a data

2016-03-09 Thread Jeff Newmiller
Your dates are incomplete (no year) so I suggest staying away from the date functions for this. Read ?regex and ?sub. x <- c( "3-Oct", "10-Nov" ) m <- sub( "^\\d+-([A-Za-z]{3})$", "\\1", x ) -- Sent from my phone. Please excuse my brevity. On March 9, 2016 10:14:25 AM PST, KMNanus wrote: >I h

[R] extracting months from a data

2016-03-09 Thread KMNanus
I have a series of dates in format 3-Oct, 10-Oct, 20-Oct, etc. I want to create a variable of just the month. If I convert the date to a character string, substr is ineffective because some of the dates have 5 characters (3-Oct) and some have 6 (10-Oct). Is there a date function that accompli