Skip to content

help with for loop: new column giving count of observation for each SITEID

3 messages · William Dunlap, arun, Bert Gunter

#
Your data was, in R-readable format (from dput())
  d <- data.frame(
       RchID = 1:9,
       site = factor(c("A", "A", "A", "B", "B", "B", "C", 
          "C", "C"), levels = c("A", "B", "C")),
       year = c(2002L, 2004L, 2005L, 2003L, 2006L, 2008L, 
          2002L, 2003L, 2004L),
       index = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L))
and I am assuming that 'index' is the desired result.  You can use
withinGroupIndex to make a new column identical to 'index'.  There
are a variety of ways to add that column to an existing data.frame,
one of which is within():
  > within(d, newIndex <- withinGroupIndex(site))
    RchID site year index newIndex
  1     1    A 2002     1        1
  2     2    A 2004     2        2
  3     3    A 2005     3        3
  4     4    B 2003     1        1
  5     5    B 2006     2        2
  6     6    B 2008     3        3
  7     7    C 2002     1        1
  8     8    C 2003     2        2
  9     9    C 2004     3        3
Or is 'index' not the desired result?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
HI,

You can also use this:res<-do.call(rbind,lapply(split(d,d$site),function(x) data.frame(x,newindex=1:nrow(x))))
?rownames(res)<-1:nrow(res)
?res
#? RchID site year index newindex
#1???? 1??? A 2002???? 1??????? 1
#2???? 2??? A 2004???? 2??????? 2
#3???? 3??? A 2005???? 3??????? 3
#4???? 4??? B 2003???? 1??????? 1
#5???? 5??? B 2006???? 2??????? 2
#6???? 6??? B 2008???? 3??????? 3
#7???? 7??? C 2002???? 1??????? 1
#8???? 8??? C 2003???? 2??????? 2
#9???? 9??? C 2004???? 3??????? 3
A.K.



----- Original Message -----
From: William Dunlap <wdunlap at tibco.com>
To: "Meredith, Christy S -FS" <csmeredith at fs.fed.us>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Tuesday, October 30, 2012 3:43 PM
Subject: Re: [R] help with for loop: new column giving count of observation for each SITEID

Your data was, in R-readable format (from dput())
? d <- data.frame(
? ? ?  RchID = 1:9,
? ? ?  site = factor(c("A", "A", "A", "B", "B", "B", "C", 
? ? ? ? ? "C", "C"), levels = c("A", "B", "C")),
? ? ?  year = c(2002L, 2004L, 2005L, 2003L, 2006L, 2008L, 
? ? ? ? ? 2002L, 2003L, 2004L),
? ? ?  index = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L))
and I am assuming that 'index' is the desired result.? You can use
withinGroupIndex to make a new column identical to 'index'.? There
are a variety of ways to add that column to an existing data.frame,
one of which is within():
? > within(d, newIndex <- withinGroupIndex(site))
? ? RchID site year index newIndex
? 1? ?  1? ? A 2002? ?  1? ? ? ? 1
? 2? ?  2? ? A 2004? ?  2? ? ? ? 2
? 3? ?  3? ? A 2005? ?  3? ? ? ? 3
? 4? ?  4? ? B 2003? ?  1? ? ? ? 1
? 5? ?  5? ? B 2006? ?  2? ? ? ? 2
? 6? ?  6? ? B 2008? ?  3? ? ? ? 3
? 7? ?  7? ? C 2002? ?  1? ? ? ? 1
? 8? ?  8? ? C 2003? ?  2? ? ? ? 2
? 9? ?  9? ? C 2004? ?  3? ? ? ? 3
Or is 'index' not the desired result?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
______________________________________________
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.
#
Eek!

Just a bit simpler would be (? la Dr. Dunlap): (d is the data frame):

d <- within(d,index <- ave(year,site, FUN = order))

(This assumes exactlly one data collection per each year that appears, though.)

Cheers,
Bert
On Tue, Oct 30, 2012 at 12:56 PM, arun <smartpink111 at yahoo.com> wrote: