Creating Observation ID

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

Creating Observation ID

Robert Baer
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]]

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

Re: Creating Observation ID

Bill.Venables
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: [hidden email] [mailto:[hidden email]] On Behalf Of Robert Baer
Sent: Tuesday, 10 May 2011 7:22 AM
To: [hidden email]
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]]

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

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

Re: Creating Observation ID

William Dunlap
In reply to this post by Robert Baer
Does the following work for you?

 > df2 <- transform(df, ObsID=ave(rep(0,length(Group)), Group,
FUN=seq_along))
 > head(df2)
        Value Group ObsID
 1 -0.0025132     B     1
 2 -1.2456156     A     1
 3 -2.0531704     B     2
 4  1.5861770     B     3
 5  0.1900908     A     2
 6  0.7197067     B     4

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Robert Baer
> Sent: Monday, May 09, 2011 2:22 PM
> To: [hidden email]
> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

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

Re: Creating Observation ID

Robert Baer
Thanks Bill and William.  Both solutions work, and both have introduced me
to whole new R functions which I'm still wrapping my head around.

What a great resource this list is.

Thanks again,
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


--------------------------------------------------
From: "William Dunlap" <[hidden email]>
Sent: Monday, May 09, 2011 5:17 PM
To: "Robert Baer" <[hidden email]>; <[hidden email]>
Subject: RE: [R] Creating Observation ID

> Does the following work for you?
>
> > df2 <- transform(df, ObsID=ave(rep(0,length(Group)), Group,
> FUN=seq_along))
> > head(df2)
>        Value Group ObsID
> 1 -0.0025132     B     1
> 2 -1.2456156     A     1
> 3 -2.0531704     B     2
> 4  1.5861770     B     3
> 5  0.1900908     A     2
> 6  0.7197067     B     4
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>> -----Original Message-----
>> From: [hidden email]
>> [mailto:[hidden email]] On Behalf Of Robert Baer
>> Sent: Monday, May 09, 2011 2:22 PM
>> To: [hidden email]
>> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>

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