John Poulsen
2008-Oct-15 19:42 UTC
[R] Removing characters and periods from character strings
Hello R-users, I have code that gives me the important variables from an analysis. I need to input these variables into a different analysis. To do this, I need to modify them slightly... 1) remove all numbers at the end of the variables, 2) remove all periods. I tried to do it with the awkward code below. It works to remove all the numbers, but when I try to remove the period everything is lost. Does anyone have a solution? And perhaps a more elegant way? pick=c("(Intercept)", "Clear", "factor(Hab)3", "factor(Hab)4", "factor(Hab)5","factor(Hab)7", "factor(Log)1", "Hunt", "Pop", "s(PrimRd).1", "s(PrimRd).2", "Unlog", "Xcoord", "Ycoord") vars=as.character(as.vector(strsplit(pick,"1"))) vars=unique(as.character(as.vector(strsplit(vars,"2")))) vars=unique(as.character(as.vector(strsplit(vars,"3")))) vars=unique(as.character(as.vector(strsplit(vars,"4")))) vars=unique(as.character(as.vector(strsplit(vars,"5")))) vars=unique(as.character(as.vector(strsplit(vars,"7")))) vars=unique(as.character(as.vector(strsplit(vars,".")))) Thanks for your help! John
Henrique Dallazuanna
2008-Oct-15 20:03 UTC
[R] Removing characters and periods from character strings
Try this; gsub("\\.|[0-9]", "", pick) On Wed, Oct 15, 2008 at 4:42 PM, John Poulsen <jpoulsen@zoo.ufl.edu> wrote:> Hello R-users, > > I have code that gives me the important variables from an analysis. I need > to input these variables into a different analysis. To do this, I need to > modify them slightly... 1) remove all numbers at the end of the variables, > 2) remove all periods. > > I tried to do it with the awkward code below. It works to remove all the > numbers, but when I try to remove the period everything is lost. > > Does anyone have a solution? And perhaps a more elegant way? > > pick=c("(Intercept)", "Clear", "factor(Hab)3", "factor(Hab)4", > "factor(Hab)5","factor(Hab)7", "factor(Log)1", "Hunt", "Pop", "s(PrimRd).1", > "s(PrimRd).2", "Unlog", "Xcoord", "Ycoord") > > vars=as.character(as.vector(strsplit(pick,"1"))) > vars=unique(as.character(as.vector(strsplit(vars,"2")))) > vars=unique(as.character(as.vector(strsplit(vars,"3")))) > vars=unique(as.character(as.vector(strsplit(vars,"4")))) > vars=unique(as.character(as.vector(strsplit(vars,"5")))) > vars=unique(as.character(as.vector(strsplit(vars,"7")))) > vars=unique(as.character(as.vector(strsplit(vars,".")))) > > Thanks for your help! > John > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Prof Brian Ripley
2008-Oct-15 20:06 UTC
[R] Removing characters and periods from character strings
A case for [g]sub: gsub(".", "", sub("[0-9]*$", "", pick), fixed = TRUE) On Wed, 15 Oct 2008, John Poulsen wrote:> Hello R-users, > > I have code that gives me the important variables from an analysis. I need > to input these variables into a different analysis. To do this, I need to > modify them slightly... 1) remove all numbers at the end of the variables, 2) > remove all periods. > > I tried to do it with the awkward code below. It works to remove all the > numbers, but when I try to remove the period everything is lost. > > Does anyone have a solution? And perhaps a more elegant way? > > pick=c("(Intercept)", "Clear", "factor(Hab)3", "factor(Hab)4", > "factor(Hab)5","factor(Hab)7", "factor(Log)1", "Hunt", "Pop", "s(PrimRd).1", > "s(PrimRd).2", "Unlog", "Xcoord", "Ycoord") > > vars=as.character(as.vector(strsplit(pick,"1"))) > vars=unique(as.character(as.vector(strsplit(vars,"2")))) > vars=unique(as.character(as.vector(strsplit(vars,"3")))) > vars=unique(as.character(as.vector(strsplit(vars,"4")))) > vars=unique(as.character(as.vector(strsplit(vars,"5")))) > vars=unique(as.character(as.vector(strsplit(vars,"7")))) > vars=unique(as.character(as.vector(strsplit(vars,".")))) > > Thanks for your help! > John > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Greg Snow
2008-Oct-15 20:07 UTC
[R] Removing characters and periods from character strings
To remove all numbers and periods at the end do:> vars <- sub("[0-9.]+$", "", pick )If there are other periods that this does not remove (not at the end) then you can do a second pass:> vars <- sub("\\.", "", vars)Part of the reason that your code for the period does not work is that an unescaped period matches any single character (hence the "\\." in the above). You may also want to look at the unique function if you want to remove the duplicates that result. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of John Poulsen > Sent: Wednesday, October 15, 2008 1:43 PM > To: r-help at r-project.org > Subject: [R] Removing characters and periods from character strings > > Hello R-users, > > I have code that gives me the important variables from an analysis. I > need to input these variables into a different analysis. To do this, I > need to modify them slightly... 1) remove all numbers at the end of the > variables, 2) remove all periods. > > I tried to do it with the awkward code below. It works to remove all > the numbers, but when I try to remove the period everything is lost. > > Does anyone have a solution? And perhaps a more elegant way? > > pick=c("(Intercept)", "Clear", "factor(Hab)3", "factor(Hab)4", > "factor(Hab)5","factor(Hab)7", "factor(Log)1", "Hunt", "Pop", > "s(PrimRd).1", "s(PrimRd).2", "Unlog", "Xcoord", "Ycoord") > > vars=as.character(as.vector(strsplit(pick,"1"))) > vars=unique(as.character(as.vector(strsplit(vars,"2")))) > vars=unique(as.character(as.vector(strsplit(vars,"3")))) > vars=unique(as.character(as.vector(strsplit(vars,"4")))) > vars=unique(as.character(as.vector(strsplit(vars,"5")))) > vars=unique(as.character(as.vector(strsplit(vars,"7")))) > vars=unique(as.character(as.vector(strsplit(vars,".")))) > > Thanks for your help! > John > > ______________________________________________ > 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.