[R] Removing characters and periods from character strings

John Poulsen jpoulsen at zoo.ufl.edu
Wed Oct 15 21:42:37 CEST 2008


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



More information about the R-help mailing list