> mynames [1] "x.y" "x.y" "x.y" "x.y" > mynames <- paste(mynames, seq_along(mynames), sep="_")
In addition, if there were a variety of names in mynames and you wanted to number each unique name separately you could use ave(): > origNames <- c("X", "Y", "Y", "X", "Z", "X") > ave(origNames, origNames, FUN=function(x)paste0(x, "_", seq_along(x))) [1] "X_1" "Y_1" "Y_2" "X_2" "Z_1" "X_3" > ave(origNames, origNames, FUN=function(x)if(length(x)==1) x else paste0(x, "_", seq_along(x))) [1] "X_1" "Y_1" "Y_2" "X_2" "Z" "X_3" Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jun 8, 2015 at 8:03 AM, Sarah Goslee <sarah.gos...@gmail.com> wrote: > I've taken the liberty of copying this back to the list, so that others can > participate in or benefit from the discussion. > > On Mon, Jun 8, 2015 at 10:49 AM, John Sorkin <jsor...@grecc.umaryland.edu> > wrote: > > > Sarah, > > I am not sure how I use check.names to replace every space in the names > of > > my variables with an underline. Can you show me how to do this? My > current > > code is as follows: > > > > check.names just tells R not to reformat your column names. If they aren't > already what you want, you'll need to do something else. > > > > data <- read.csv("C:\\Users\\john\\Dropbox > > (Personal)\\HanlonMatt\\fullgenus3.csv") > > > > The problem I has is that my column names are not unique, e.g., I have > > multiple columns whose column names are (in CSV format): > > X Y, X Y, X Y, X Y > > R reads the names as follows: > > X.Y, X.Y.1, X.Y.2, X.Y.3 > > I need to have the names look like: > > X_Y, X_Y.1, X_Y.2, X_Y.3 > > > > You've been saying that you want to replace every space with an underscore, > but that's not what your example shows. Instead, you want to let R import > the names and add the identifying number (though if you do it yourself you > can get the number to match the column number, which is neater), then > change the FIRST underscore to a period. > > I'd import them with check.names=FALSE, then modify them explicitly: > > > > mynames <- c("x y", "x y", "x y", "x y") > > mynames > [1] "x y" "x y" "x y" "x y" > > mynames <- sub(" ", ".", mynames) > > mynames > [1] "x.y" "x.y" "x.y" "x.y" > > mynames <- paste(mynames, seq_along(mynames), sep="_") > > mynames > [1] "x.y_1" "x.y_2" "x.y_3" "x.y_4" > > > You could also let R modify them, then use sub() to change the first > underscore to a period and leave the rest alone. > > Sarah > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.