Hi everybody, I have a vector of characters and i would like to extract certain parts. My vector is named metr_list: [1] "F:/Naval_Live_Oaks/2005/data//BE.tif" [2] "F:/Naval_Live_Oaks/2005/data//CH.tif" [3] "F:/Naval_Live_Oaks/2005/data//CRR.tif" [4] "F:/Naval_Live_Oaks/2005/data//HOME.tif" And i would like to extract BE, CH, CRR, and HOME in a different vector named "names.id" for example. I read the help files for sub and grep and the likes but i have to recognize that i did not understand it. So i've done this (which does the job but extremely clumsy): b <- strsplit(metr_list, "//") b <- unlist(b) d <- strsplit(b, "\\.") d <- unlist(d) names.id <- d[c(2, 5, 8, 11)] Can anybody show what would be the proper way to achieve this with some explanations? Thanks, Monica _________________________________________________________________ Hotmail? goes with you. ial_Mobile1_052009
They look like file path, so you can make use of basename() first, then use gsub to strip the suffix.> x<-c("F:/Naval_Live_Oaks/2005/data//BE.tif","F:/Naval_Live_Oaks/2005/data//CH.tif") > x2<-sapply(x,basename,USE.NAMES=FALSE) > gsub("[.].{1,}$","",x2)[1] "BE" "CH" Ronggui 2009/5/26 Monica Pisica <pisicandru at hotmail.com>:> > Hi everybody, > > I have a vector of characters and i would like to extract certain parts. My vector is named metr_list: > > [1] "F:/Naval_Live_Oaks/2005/data//BE.tif" > [2] "F:/Naval_Live_Oaks/2005/data//CH.tif" > [3] "F:/Naval_Live_Oaks/2005/data//CRR.tif" > [4] "F:/Naval_Live_Oaks/2005/data//HOME.tif" > > And i would like to extract BE, CH, CRR, and HOME in a different vector named "names.id" for example. I read the help files for sub and grep and the likes but i have to recognize that i did not understand it. So i've done this (which does the job but extremely clumsy): > > b <- strsplit(metr_list, "//") > b <- unlist(b) > d <- strsplit(b, "\\.") > d <- unlist(d) > names.id <- d[c(2, 5, 8, 11)] > > Can anybody show what would be the proper way to achieve this with some explanations? > > Thanks, > > Monica > _________________________________________________________________ > Hotmail? goes with you. > > ial_Mobile1_052009 > ______________________________________________ > 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. >-- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html
Try this: sub(".tif$", "", basename(metr_list)) On Tue, May 26, 2009 at 9:27 AM, Monica Pisica <pisicandru at hotmail.com> wrote:> > Hi everybody, > > I have a vector of characters and i would like to extract certain parts. My vector is named metr_list: > > [1] "F:/Naval_Live_Oaks/2005/data//BE.tif" > [2] "F:/Naval_Live_Oaks/2005/data//CH.tif" > [3] "F:/Naval_Live_Oaks/2005/data//CRR.tif" > [4] "F:/Naval_Live_Oaks/2005/data//HOME.tif" > > And i would like to extract BE, CH, CRR, and HOME in a different vector named "names.id" for example. I read the help files for sub and grep and the likes but i have to recognize that i did not understand it. So i've done this (which does the job but extremely clumsy): > > b <- strsplit(metr_list, "//") > b <- unlist(b) > d <- strsplit(b, "\\.") > d <- unlist(d) > names.id <- d[c(2, 5, 8, 11)] > > Can anybody show what would be the proper way to achieve this with some explanations? > > Thanks, > > Monica > _________________________________________________________________ > Hotmail? goes with you. > > ial_Mobile1_052009 > ______________________________________________ > 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. >
Monica Pisica wrote:> Hi everybody, > > I have a vector of characters and i would like to extract certain parts. My vector is named metr_list: > > [1] "F:/Naval_Live_Oaks/2005/data//BE.tif" > [2] "F:/Naval_Live_Oaks/2005/data//CH.tif" > [3] "F:/Naval_Live_Oaks/2005/data//CRR.tif" > [4] "F:/Naval_Live_Oaks/2005/data//HOME.tif" > > And i would like to extract BE, CH, CRR, and HOME in a different vector named "names.id"one way that seems reasonable is to use sub: output = sub('.*//(.*)[.]tif$', '\\1', input) which says 'from each string remember the substring between the rigthmost two slashes and a .tif extension, exclusive, and replace the whole thing with the captured part'. if the pattern does not match, you get the original input: sub('.*//(.*)[.]tif$', '\\1', 'f:/foo/bar//buz.tif') # buz vQ