Hosack, Michael
2010-Mar-15 17:45 UTC
[R] Assigning a sequence to a subsetted data frame variable
Hey folks, I have a difficult (at least for me) problem that I was hoping one of you may know how to solve. I want to assign a sequence to subsets of a variable in a data frame based on date. The variable is 'SITE1' and the date is a unique day (DD) and month (MM) combination. The sequence contains site numbers 101:104, and each day-month combination takes four site numbers from that sequence in an order dependent upon which site number is listed. For example, I need a sequence of 101,102,103,104 if 101 is the number listed, 102,103,104,101 if 102 is the number listed, 104,101, 102,103 if the 104 is the number listed for a given DD MM combination, etc. As a further example, for every sequence that happens to be 102,102,102,102 for a MM DD combination I want to replace it with 102,103,104,101. Please see the attached partial dataframe. Any help would be greatly appreciated. I have tried creating a function using seq() and then using ddply, and also writing a loop with no luck. Mike -------------- next part -------------- A non-text attachment was scrubbed... Name: Dataframe.pdf Type: application/pdf Size: 76576 bytes Desc: Dataframe.pdf URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100315/a0c7cdf1/attachment.pdf>
jim holtman
2010-Mar-15 19:16 UTC
[R] Assigning a sequence to a subsetted data frame variable
try this:> x <- data.frame(day=rep(1:10, each=4), site=rep(sample(101:104, 10, TRUE),each=4))> s.f <- function(start){+ start <- start[1] + 0:3 # generate the sequence + ifelse(start > 104, start - 4, start) + }> x$siteNew <- ave(x$site, x$day, FUN=s.f) > xday site siteNew 1 1 104 104 2 1 104 101 3 1 104 102 4 1 104 103 5 2 101 101 6 2 101 102 7 2 101 103 8 2 101 104 9 3 103 103 10 3 103 104 11 3 103 101 12 3 103 102 13 4 101 101 14 4 101 102 15 4 101 103 16 4 101 104 17 5 102 102 18 5 102 103 19 5 102 104 20 5 102 101 21 6 102 102 22 6 102 103 On Mon, Mar 15, 2010 at 1:45 PM, Hosack, Michael <mhosack@state.pa.us>wrote:> Hey folks, > > I have a difficult (at least for me) problem that I was hoping > one of you may know how to solve. I want to assign a sequence > to subsets of a variable in a data frame based on date. > The variable is 'SITE1' and the date is a unique day (DD) and > month (MM) combination. The sequence contains site numbers 101:104, > and each day-month combination takes four site numbers from that > sequence in an order dependent upon which site number is listed. > For example, I need a sequence of 101,102,103,104 if 101 is the > number listed, 102,103,104,101 if 102 is the number listed, 104,101, > 102,103 if the 104 is the number listed for a given DD MM combination, > etc. As a further example, for every sequence that happens to be > 102,102,102,102 > for a MM DD combination I want to replace it with 102,103,104,101. > Please see the attached partial dataframe. Any help would be > greatly appreciated. I have tried creating a function using seq() > and then using ddply, and also writing a loop with no luck. > > Mike > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]