Re: [R] Creating Observation ID

From: <Bill.Venables_at_csiro.au>
Date: Tue, 10 May 2011 07:48:17 +1000

Here is one way:

df <- data.frame(Value = rnorm(30),

                 Group = sample(c('A','B','C'), 30,
                 replace = TRUE))

## make a little function to do the job
iNumber <- function(f) {
  f <- as.factor(f)
  X <- outer(f, levels(f), "==")+0
  rowSums(X * apply(X, 2, cumsum))
}

## add the numbering column
df <- within(df, internalNumber <- iNumber(Group))

## Check that it works
> head(df)

       Value Group internalNumber
1 -1.5014788     C              1
2  0.6035679     C              2
3 -0.6953930     C              3
4 -0.2413863     A              1
5 -0.1170961     A              2
6  1.5110721     C              4 

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Robert Baer Sent: Tuesday, 10 May 2011 7:22 AM
To: R-help_at_r-project.org
Subject: [R] Creating Observation ID

If I have a data frame something like:
Value=rnorm(30)
Group = sample(c('A','B','C'), 30, replace=TRUE) df = data.frame(Value, Group)

It seems like it should be simple to create an 'ObsID' column which indicates the observation order of each Value within each of the 3 groups. Somehow, I can't quite see how to do it without manually sub-setting the parent data frame and then putting it back together again.

Anyone able to get me started on a cleaner (more R-like) approach?

Thanks,

Rob



Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965         [[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
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.

R-help_at_r-project.org mailing list
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. Received on Tue 10 May 2011 - 02:52:41 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 May 2011 - 03:00:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive