[R] help with for loop: new column giving count of observation for each SITEID
William Dunlap
wdunlap at tibco.com
Tue Oct 30 20:07:03 CET 2012
Is this what you want?
> withinGroupIndex <- function(group, ...) ave(integer(length(group)), group, ..., FUN=seq_along)
> site <- c("A","A","C","D","C","A","B")
> data.frame(site, index=withinGroupIndex(site))
site index
1 A 1
2 A 2
3 C 1
4 D 1
5 C 2
6 A 3
7 B 1
You can add more arguments if the groups depend on more than one value:
> year <- rep(c(1985, 2012), c(4,3))
> data.frame(site, year, index=withinGroupIndex(site, year))
site year index
1 A 1985 1
2 A 1985 2
3 C 1985 1
4 D 1985 1
5 C 2012 1
6 A 2012 1
7 B 2012 1
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Meredith, Christy S -FS
> Sent: Tuesday, October 30, 2012 11:17 AM
> To: r-help at R-project.org
> Subject: [R] help with for loop: new column giving count of observation for each SITEID
>
>
> Hello,
> I think this is easy, but I can't seem to find a good way to do this in the R help. I have a list
> of sites, with multiple years of data for each site id. I want to create a new column that
> gives a number describing whether it is the 1st year ("1" ) the data was collected for the
> site, the second year ("2"), etc. I have different years for each siteid, but I don't care
> which year it was collected, just the order that it is in for that siteid. This is what I have
> so far, but it doesn't do the analysis separately for each SiteID.
>
> indexi<-indexg[order(indexg$SiteID,indexg$Yr),]
>
> obs=0
> indexi=na.omit(indexi)
> for(i in 1:length(indexi$SiteID)){
> obs=obs+1
> indexi$obs[i]=obs
> }
>
>
> Thanks for any help you can give.
>
> Christy Meredith
> USDA Forest Service
> Rocky Mountain Research Station
> PIBO Monitoring
> Data Analyst
> Voice: 435-755-3573
> Fax: 435-755-3563
>
>
>
>
>
> This electronic message contains information generated by the USDA solely for the
> intended recipients. Any unauthorized interception of this message or the use or
> disclosure of the information it contains may violate the law and subject the violator to
> civil or criminal penalties. If you believe you have received this message in error, please
> notify the sender and delete the email immediately.
>
> [[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.
More information about the R-help
mailing list