On Apr 23, 2013, at 12:13 PM, arun wrote: > Hi, > Try this: > set.seed(25) > dat1<- > data.frame(ID=c(10011001,10011004,20012001,2022001,12002),val=rnorm(5),stringsAsFactors=FALSE) > > dat2<- dat1 > library(stringr) > dat1$ID<-as.numeric(str_sub(dat1$ID,-4,-1)) > dat1 > # ID val > #1 1001 -0.2118336 > #2 1004 -1.0415911 > #3 2001 -1.1533076 > #4 2001 0.3215315 > #5 2002 -1.5001299 > > > #or > > dat2$ID<-as.numeric(gsub(".*(\\d{4})$","\\1",dat2$ID)) > dat2 > # ID val > #1 1001 -0.2118336 > #2 1004 -1.0415911 > #3 2001 -1.1533076 > #4 2001 0.3215315 > #5 2002 -1.5001299
Could also just use substr(). I wondered if offering a factor argument to either substr() or to nchar() would be a problem but they seem to apply the expected coercion: > dat1$last4 <- substr( dat1$ID, nchar(dat1$ID)-3, nchar(dat1$ID) ) > dat1 ID val last4 1 10011001 0.6328586 1001 2 10011004 -0.1178190 1004 3 20012001 -1.2437550 2001 4 2022001 0.6841814 2001 5 12002 -1.3725696 2002 -- David. > > A.K. > >> Hi, >> >> How about a more complicated case (maybe simple to most of you :) )? >> >> 10011001, 10011004, 20012001, 40012034, etc., >> >> I would like to only keep the last 4 digits of each number. >> >> How should I do it? >> >> Thanks, >> >> York >> > >> ----- Original Message ----- >> From: arun <smartpink...@yahoo.com> >> To: R help <r-help@r-project.org> >> Cc: >> Sent: Tuesday, April 23, 2013 12:10 PM >> Subject: Re: Extract part of a numer >> >> Hi, >> May be this helps: >> set.seed(25) >> dat1<- >> data.frame(ID=c("1001#01","1001#02","1001#03","1002#01","1002#02"),val=rnorm(5),stringsAsFactors=FALSE) > >dat1$ID<-as.numeric(gsub("#.*","",dat1$ID)) > >dat1 >> # ID val >> #1 1001 -0.2118336 >> #2 1001 -1.0415911 >> #3 1001 -1.1533076 >> #4 1002 0.3215315 >> #5 1002 -1.5001299 >> A.K. >> >> >> Hi all, >> >> I have a database with a colume ID have the ID numbers like this1001#01, >> 1001#02, etc.. >> >> How could I do to just extract 1001 out? >> >> Thanks, >> >> York > > ______________________________________________ > 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. David Winsemius Alameda, CA, USA ______________________________________________ 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.