[R] R code helps needed!
Jim Lemon
drjimlemon at gmail.com
Thu Mar 2 23:42:13 CET 2017
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.
More information about the R-help
mailing list