[R] R code helps needed!

SH emptican at gmail.com
Fri Mar 3 14:00:36 CET 2017


Hi Jim,

Thank you very much for replying back.

I think the data I presented have not many 'pass' than I thought.  The
purpose of the code is to skip sampling for 5 consecutive rows when a
previous row is found as 'pass'.  Thus, because the fourth row is
'pass', sampling will be skipped next five rows (i.e., from 5th to 9th
rows).  Therefore any 'pass' within next 5 rows after first 'pass' should
not affect 'skip'.  Could you try this?  Based on your code, I
guess 'return' function may be one I should search.  I haven't used it
before so I am not familiar with the function.  I made a new data set with
'expected.decision' column.  In the data set, once a 'pass' is found, the
next sampling starts 5 rows after.  For example, since the forth row is
'pass',  the next sampling starts at 10th row.  Although 6th row should be
'pass', I want to label them as 'skip' since no sampling is made.

The objective of the study is to investigate how many of 'reject' rows get
'skip' with a given sampling scheme, the rate of 'pass' because of skip
sampling which should be 'reject'.

Could you also try this data and give me your feedback?  Thanks again for
you helps!!!

Steve

result<-read.table(text=
    "intercept decision expected.decision
 1 reject reject
 2 reject reject
 3 reject reject
 0 pass pass
 3 reject skip
 0 pass skip
 3 reject skip
 5 reject skip
0 pass skip
 0 pass pass
3 reject skip
 1 reject skip
 0 pass skip
 0 pass skip
 2 reject skip
 1 reject reject
 0 pass pass
 3 reject skip
 0 pass skip
 2 reject skip
 0 pass skip
 1 reject skip
 2 reject reject
 2 reject reject
",
  header=TRUE,stringsAsFactors=FALSE)
 passes<-which(result$intercept == 0)
 skips<-as.vector(sapply(passes,function(x) return(x+1:5)))
 result$decision[skips]<-"skip"
result



On Thu, Mar 2, 2017 at 5:42 PM, Jim Lemon <drjimlemon at gmail.com> wrote:

> Hi Steve,
> Try this:
>
> result<-read.table(text=
>    "intercept decision
>  1       reject
>  2       reject
>  3       reject
>  0       pass
>  3       reject
>  2       reject
>  3       reject
>  5       reject
>  3       reject
>  1       reject
>  1       reject
>  2       reject
>  2       reject
>  0       pass
>  3       reject
>  3       reject
>  2       reject
>  2       reject
>  1       reject
>  1       reject
>  2       reject
>  2       reject",
>  header=TRUE,stringsAsFactors=FALSE)
> passes<-which(result$intercept == 0)
> skips<-as.vector(sapply(passes,function(x) return(x+1:5)))
> result$decision[skips]<-"skip"
>
> Note that result$decision must be a character variable for this to
> work.If it is a factor, convert it to character.
>
> Jim
>
>
> On Thu, Mar 2, 2017 at 11:54 PM, SH <emptican at gmail.com> wrote:
> > Hi
> >
> > Although I posted this in stackoverflow yesterday, I am asking here to
> get
> > helps as soon as quickly.
> >
> > I need help make code for mocking sampling environment. Here is my code
> > below:
> >
> > First, I generated mock units with 1000 groups of 100 units. Each row is
> > considered as independent sample space.
> >
> > unit <- 100 # Total units
> > bad.unit.rate <- .05 # Proportion of bad units
> > bad.unit.num <- ceiling(unit*bad.unit.rate) # Bad units
> > n.sim=1000
> > unit.group <- matrix(0, nrow=n.sim, ncol=unit)for(i in 1:n.sim){
> >     unit.group[i, ] <- sample(rep(0:1, c(unit-bad.unit.num,
> bad.unit.num)))}
> > dim(unit.group)
> >
> > It gives 1000 by 100 groups
> >
> > ss <- 44 # Selected sample size
> >
> > 44 out of 100 units will be selected and decision (pass or reject) will
> be
> > made based on sampling.
> >
> > This below is decision code:
> >
> > intercept <- rep(0, nrow(unit.group))
> > decision <- rep(0, nrow(unit.group))
> > set.seed(2017)for(i in 1:nrow(unit.group)){
> >     selected.unit <- sample(1:unit, ss)
> >     intercept[i] <- sum(unit.group[i,][selected.unit])
> >     decision[i] <- ifelse(intercept[i]==0, 'pass', 'reject')
> >     result <- cbind(intercept, decision)
> >     result}
> > dim(result)
> > head(result, 30)
> >
> >> head(result, 30)
> >       intercept decision
> >  [1,] "1"       "reject"
> >  [2,] "2"       "reject"
> >  [3,] "3"       "reject"
> >  [4,] "0"       "pass"
> >  [5,] "3"       "reject"
> >  [6,] "2"       "reject"
> >  [7,] "3"       "reject"
> >  [8,] "5"       "reject"
> >  [9,] "3"       "reject"
> > [10,] "1"       "reject"
> > [11,] "1"       "reject"
> > [12,] "2"       "reject"
> > [13,] "2"       "reject"
> > [14,] "0"       "pass"
> > [15,] "3"       "reject"
> > [16,] "3"       "reject"
> > [17,] "2"       "reject"
> > [18,] "2"       "reject"
> > [19,] "1"       "reject"
> > [20,] "1"       "reject"
> > [21,] "2"       "reject"
> > [22,] "2"       "reject"
> >
> > I was able to make a decision for each 1000 rows based on sampling as
> above.
> >
> > Now, I want to make code for "second" decision option as follows.
> Assuming
> > the row number is in order of time or sequence, if 'intercept' value is 0
> > or 'decision' is 'pass' in the row 4 above, I want to skip any decision
> > next following 5 (or else) and to label as 'skip', not 'reject'. In the
> > example above, rows from 5 to 9 will be 'skip' than 'reject'. Also, rows
> > from 15 to 19 should be 'skip' instead of 'reject'. Although I tried to
> > make preliminary code with my post, I have no idea where to start. Could
> > anyone help me to make code? Any feedback will be greatly appreciated.
> >
> > Thank you very much in advance!!!
> >
> > Steve
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list