[R] conditional seq

mathijsdevaan mathijsdevaan at gmail.com
Wed Feb 23 21:51:11 CET 2011


Thanks, learned something new again. About my second question:

You're right, the input rows are similar, but "cumsum(z) - z" generates
dissimilar output rows. The rule I want to define is: if input rows are
repeated, replace the output row with the minimum value of the output row.
So (the difference is found in Cell(11,7)):

   A B    C D E F G 
1  1 a 1999 1 0 0 0 
7  3 a 2004 0 1 1 0 
2  1 b 1999 0 1 0 0 
10 4 b 2001 1 0 0 1 
8  3 b 2004 0 1 1 1 
3  1 c 1999 0 1 0 0 
5  2 c 2001 1 0 0 1 
11 4 c 2001 1 0 1 1 
4  1 d 1999 1 0 0 0 
6  2 d 2001 0 1 1 0 
12 4 d 2001 0 1 1 0
9  3 d 2004 0 1 1 2

Instead of:

   A B    C D E F G 
1  1 a 1999 1 0 0 0 
7  3 a 2004 0 1 1 0 
2  1 b 1999 0 1 0 0 
10 4 b 2001 1 0 0 1 
8  3 b 2004 0 1 1 1 
3  1 c 1999 0 1 0 0 
5  2 c 2001 1 0 0 1 
11 4 c 2001 1 0 1 1 
4  1 d 1999 1 0 0 0 
6  2 d 2001 0 1 1 0 
12 4 d 2001 0 1 1 1
9  3 d 2004 0 1 1 2

Thanks,

Mathijs


Joshua Wiley-2 wrote:
> 
> On Wed, Feb 23, 2011 at 8:32 AM, mathijsdevaan <mathijsdevaan at gmail.com>
> wrote:
>> Hi,
>>
>> I have two questions:
>> 1. How do I combine "DF$F =" and "DF$G =" into one function? (The
>> original
>> dataset contains many more columns for which I want to execute the same
>> operation)
> 
> Just define a function that can handle multiple columns.  For instance:
> 
> foo <- function(x) {
>   unlist(lapply(x, FUN = function(z) cumsum(z) - z))
> }
> 
> ## now down to one step
> ave(DF[, c("D", "E")], DF$B, FUN = foo)
> 
>> 2. How do I improve the ave function so that the value DF(12,G) = 0
>> instead
>> of 1 (see bold font)? Both DF(12,B)=DF(6,B) and DF(12,C)=DF(6,C), so I
>> cannot distinguish between both rows, which is why I want DF(12,G) = 0,
>> leaving DF(9,G) = 2.
> 
> Bold font does not show up on the email listserv, so I do not follow
> what you mean by "improve" ave().  If both the input rows are equal,
> shouldn't the output rows also be equal?  If not, you need to clearly
> define the rules in the cases where multiple input rows are equal.
> 
>>
>> Please consider the example below:
> 
> Thanks for providing an easy to use dataset!
> 
> Best regards,
> 
> Josh
> 
> [snip]
> 
> -- 
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.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.
> 
> 

-- 
View this message in context: http://r.789695.n4.nabble.com/conditional-seq-tp3321327p3321696.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list