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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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

Many thanks!

Stephen

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to make row.names based on column1 with duplicated values

Jeff Newmiller
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:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to make row.names based on column1 with duplicated values

PIKAL Petr
Hi

You probably meant
make.unique( gene, sep="-" )
[1] "a"   "b"   "c"   "d"   "c-1" "d-1" "c-2" "f"

I second your objection to such name tweeking.

Cheers
Petr

> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Jeff
> Newmiller
> Sent: Thursday, March 1, 2018 9:13 AM
> To: Stephen HonKit Wong <[hidden email]>
> Cc: [hidden email]
> Subject: Re: [R] how to make row.names based on column1 with duplicated
> values
>
> 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,5
> > 5,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:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
>                                        Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>
> ______________________________________________
> [hidden email] 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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to make row.names based on column1 with duplicated values

R help mailing list-2
In reply to this post by Honkit Wong
You can do this with ave():

gene <- c("a","b","c","d","c","d","c","f")
ave(gene, gene, FUN=function(x)if(length(x)>1)paste(x,seq_along(x),sep="-")
else x)
# [1] "a"   "b"   "c-1" "d-1" "c-2" "d-2" "c-3" "f"

You can probably speed it up a bit by pulling the paste() out of FUN
and doing it later.  It would be simpler if you put the '-N' after all
genes,
not just the ones that were not repeated.



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Feb 28, 2018 at 10:18 PM, Stephen HonKit Wong <[hidden email]>
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
>
> Many thanks!
>
> Stephen
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.