[R] how to make row.names based on column1 with duplicated values

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Thu Mar 1 09:12:44 CET 2018


On Wed, 28 Feb 2018, Stephen HonKit Wong wrote:

> Dear All,
> Suppose I have a dataframe like this with many thousands rows all with
> different names:
> data.frame(gene=c("a","b","c","d","c","d","c","f"),value=c(20,300,48,55,9,2,100,200)),
>
> I want to set column "gene" as row.names, but there are duplicates (c, d),
> which I want to transform into this as row names: a, b, c-1, d-1, c-2, d-2,
> c-3, f

a) My reaction is that this doesn't actually sound like a very useful 
thing to do, so I caution you to beware of trying to accomplish too much 
with this capability... if it continues to be difficult to do what you 
want once you have these row names that appear nowhere else in your data, 
start looking for other ways to obtain the actual answers you want.

b) If your data frame is stored in `dta`, then you can do

rownames( dta ) <- make.names( dta$gene, unique = TRUE, sep="-" )

---------------------------------------------------------------------------
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



More information about the R-help mailing list