If they are all formatted as your example, substr() would be simpler: MONTH <- c("2012-07", "2001-07", "2002-01") QUARTER <- c("2012-3", "2001-3", "2002-1") YEAR <- c(2013, 2001, 2002) Inp <- data.frame(MONTH, QUARTER, YEAR) Out <- data.frame(MONTH=substr(MONTH, 6, 8), QUARTER=substr(QUARTER, 6, 7), YEAR)
This assumes MONTH and QUARTER are character strings and not dates. ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Fred G > Sent: Tuesday, July 24, 2012 12:36 PM > To: r-help@r-project.org > Subject: [R] Regular Expression > > Hi-- > > I have three columns in an input file: > MONTH QUARTER YEAR > 2012-07 2012-3 2012 > 2001-07 2001-3 2001 > 2002-01 2002-1 2002 > > I want to make output like so: > MONTH QUARTER YEAR > 07 3 2012 > 07 3 2001 > 01 1 2002 > > I was having some trouble getting the regular expression to work. I > think > it should be something like the following: > tmp <- uncurated$MONTH > *tmp <- gsub("[^-\\d\\d]","",tmp,perl=TRUE)* > *tmp[tmp=="-"] <- ""* > *curated$MONTH <- tmp* > * > * > tmp <- uncurated$QUARTER > *tmp <- gsub("[^-\\d]","",tmp,perl=TRUE)* > *tmp[tmp=="-"] <- ""* > *curated$QUARTER <- tmp* > * > * > *but it's not quite working. I want to be able to isolate any digits > that > occur after the hyphen and to delete everything before and including > the > hyphen. Would greatly appreciate any clarification anyone can provide.* > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.