s-news
[Top] [All Lists]

Re: factor column in a data frame

To: "Khan, Sohail" <khan@cshl.edu>
Subject: Re: factor column in a data frame
From: "Dimitris Rizopoulos" <dimitris.rizopoulos@med.kuleuven.be>
Date: Mon, 20 Mar 2006 18:18:13 +0100
Cc: <s-news@lists.biostat.wustl.edu>
References: <C8696843AE995F4EA4CDC3E2B83482A901878FBB@mailbox02.cshl.edu>
you may consider something like the following:

dat <- data.frame(ID = c("a", "a", "a", "b", "b", "c", "c", "c", "d"), X = rnorm(9))
###########################
rl <- rle(dat$ID)$lengths
dat$ID <- factor(paste(id, "-", unlist(lapply(rl, seq, from = 1)), sep = ""))
dat


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
    http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- From: "Khan, Sohail" <khan@cshl.edu>
To: <s-news@lists.biostat.wustl.edu>
Sent: Monday, March 20, 2006 5:59 PM
Subject: [S] factor column in a data frame


Dear List,

I have a data frame as follows:

ID         Ratio
a 2.5
a 3.5
a 1.3
b 2.9
b 3.9
c 5.2
c 3.6

I would like to count the factors in the ID column and label them as such, so they look like:
ID         Ratio
a-1 2.5
a-2 3.5
a-3 1.3
b-1 2.9
b-2 3.9
c-1 5.2
c-2 3.6

Thanks for any suggestions/advice.


Sohail Khan
Scientific Programmer
COLD SPRING HARBOR LABORATORY
Genome Research Center
500 Sunnyside Boulevard
Woodbury, NY 11797
(516)422-4076

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm


<Prev in Thread] Current Thread [Next in Thread>