[R] coding logic and syntax in R

Adaikalavan RAMASAMY ramasamya at gis.a-star.edu.sg
Fri Dec 26 07:01:50 CET 2003


In addition to the previous replies, try this

x <- as.numeric(strsplit("111110111111011111111111111", NULL)[[1]])
g <- rep(1:3, each=9)                   # set numbering
rbind(x, g)                             # to check

y <- unlist( tapply(x, g, cummin) )
> y
11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 
 1  1  1  1  1  0  0  0  0  1  1  1  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1 

tapply() applies a given function, in this case cummin(), to the sets defined by 'g'.
cummin() returns the cummulative minimum
Here, the names of vector y is a combination of set number and observation in set number.

--
Adaikalavan Ramasamy 

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Renaud Lancelot
Sent: Wednesday, December 24, 2003 5:00 PM
To: Pravin
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] coding logic and syntax in R


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)

______________________________________________
R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list