[R] coding logic and syntax in R

Renaud Lancelot renaud.lancelot at pasteur.mg
Wed Dec 24 10:00:22 CET 2003


Pravin a écrit :

> Hello,
> 
>  
> 
> I am a beginner in R programming and recently heard about this mailing list.
> Currently, I am trapped into a simple problem for which I just can't find a
> solution. I have a huge dataset (~81,000 observations) that has been
> analyzed and the final result is in the form of 0 and 1(one column). 
> 
>  
> 
> I need to write a code to process this column in a little complicated way. 
> 
> These 81,000 observations are actually 9,000 sets (81,000/9). 
> 
> So, in each set whenever zero appears, rest all observations become zero.
> 
>  
> 
> For example;
> 
> If the column has: 
> 
> 111110111111011111111111111111111....
> 
> The output should look like: 
> 
> 111110000111000000111111111111111...
> 
>  
> 
> I hope this makes sense.
> 
>  
> 
> Thank you in anticipation,
> 
>  
> 
> Pravin
> 
>  
> 
> Pravin Jadhav
> 
>  
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> 
Pravin a écrit :

 > Hello,
 >
 >
 >
 > I am a beginner in R programming and recently heard about this 
mailing list.
 > Currently, I am trapped into a simple problem for which I just can't 
find a
 > solution. I have a huge dataset (~81,000 observations) that has been
 > analyzed and the final result is in the form of 0 and 1(one column).
 >
 >
 >
 > I need to write a code to process this column in a little complicated 
way.
 >
 > These 81,000 observations are actually 9,000 sets (81,000/9).
 >
 > So, in each set whenever zero appears, rest all observations become zero.
 >
 >
 >
 > For example;
 >
 > If the column has:
 >
 > 111110111111011111111111111111111....
 >
 > The output should look like:
 >
 > 111110000111000000111111111111111...
 >
 >
 >
 > I hope this makes sense.
 >
 >
 >
 > Thank you in anticipation,
 >
 >
 >
 > Pravin
 >
 >
 >
 > Pravin Jadhav
 >
 >
 >
 >
 > 	[[alternative HTML version deleted]]
 >
 > ______________________________________________
 > R-help at stat.math.ethz.ch mailing list
 > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
 >
 >
Here is an example:

set.seed(101)
v <- sample(c(0, 1), size = 36, replace = TRUE, prob = c(.05, .95))
L <- length(v) / 9
idx <- rep(seq(L), each = 9)

fn <- function(x){
         ok <- FALSE
         for(i in seq(length(x))){
           if(x[i] == 0) ok <- TRUE
           x[i] <- if(ok) 0 else 1
           }
         x
   }

cbind(idx, v, recod = unlist(tapply(v, idx, fn)))
    idx v recod
11   1 1     1
12   1 1     1
13   1 1     1
14   1 1     1
15   1 1     1
16   1 1     1
17   1 1     1
18   1 1     1
19   1 1     1
21   2 1     1
22   2 1     1
23   2 1     1
24   2 1     1
25   2 1     1
26   2 1     1
27   2 1     1
28   2 1     1
29   2 1     1
31   3 1     1
32   3 1     1
33   3 1     1
34   3 0     0
35   3 1     0
36   3 1     0
37   3 1     0
38   3 1     0
39   3 1     0
41   4 1     1
42   4 1     1
43   4 1     1
44   4 1     1
45   4 1     1
46   4 1     1
47   4 1     1
48   4 1     1
49   4 1     1
 >

Merry Christmas !

Renaud


-- 
Dr Renaud Lancelot
vétérinaire épidémiologiste
Ambassade de France - SCAC
BP 834 Antannarivo 101
Madagascar

tél. +261 (0)32 04 824 55 (cell)
      +261 (0)20 22 494 37 (home)




-- 
Dr Renaud Lancelot
vétérinaire épidémiologiste
Ambassade de France - SCAC
BP 834 Antannarivo 101
Madagascar

tél. +261 (0)32 04 824 55 (cell)
      +261 (0)20 22 494 37 (home)




More information about the R-help mailing list