#I want to remove '_alpha' in a vector of strings myInput = c( "afg3_alpha", "alg12_alpha", "dbp3_alpha", "elp4_alpha", "fob1_alpha", "gpr1_alpha") #my current solution tmpsplit = strsplit(myInput, '_') tmp = NA for (item in tmpsplit){ tmp = c(tmp, item[1]) } results1 = tmp[-1]; #this is what I need, but I want a better solution. #Here is what I come up with, but it is pretty awkward tmp3 = lapply(myInput, FUN=function(x){strsplit(x, '_')[[1]][1]}) results2 = unlist(tmp3) #Please help me revise the above loop. Thanks, -- Hong [[alternative HTML version deleted]]
?sub --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. CompBiol ATL <alongway at gmail.com> wrote:>#I want to remove '_alpha' in a vector of strings >myInput = c( "afg3_alpha", "alg12_alpha", "dbp3_alpha", "elp4_alpha", > "fob1_alpha", "gpr1_alpha") > >#my current solution >tmpsplit = strsplit(myInput, '_') >tmp = NA >for (item in tmpsplit){ > tmp = c(tmp, item[1]) >} >results1 = tmp[-1]; >#this is what I need, but I want a better solution. > >#Here is what I come up with, but it is pretty awkward >tmp3 = lapply(myInput, FUN=function(x){strsplit(x, '_')[[1]][1]}) >results2 = unlist(tmp3) > >#Please help me revise the above loop. Thanks, -- Hong > > [[alternative HTML version deleted]] > >______________________________________________ >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.
gsub("_.*","",myInput) #[1] "afg3"? "alg12" "dbp3"? "elp4"? "fob1"? "gpr1" A.K. ----- Original Message ----- From: CompBiol ATL <alongway at gmail.com> To: r-help at r-project.org Cc: Hong Qin <qinstat at gmail.com> Sent: Monday, June 17, 2013 6:52 PM Subject: [R] help with string split in R #I want to remove '_alpha' in a vector of strings myInput = c( "afg3_alpha", "alg12_alpha", "dbp3_alpha",? "elp4_alpha", "fob1_alpha",? "gpr1_alpha") #my current solution tmpsplit = strsplit(myInput, '_') tmp = NA for (item in tmpsplit){ ? tmp = c(tmp, item[1]) } results1? = tmp[-1]; #this is what I need, but I want a better solution. #Here is what I come up with, but it is pretty awkward tmp3 = lapply(myInput, FUN=function(x){strsplit(x, '_')[[1]][1]}) results2 = unlist(tmp3) #Please help me revise the above loop. Thanks, -- Hong ??? [[alternative HTML version deleted]] ______________________________________________ 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.