Re: [R] Nested for loop

2017-08-08 Thread S Ellison
> The code I've attached works for a population of 400 and samples 100 times.
> I'd like to extend this to 300 samples and 3 populations. So, the x-axis would
> range from 0-300 samples.
> 
> What I'm having trouble with is finding a way to change the population mid-
> way through the function. I want samples 1-100 to be taken from a
> population of 400, samples 101-200 to be taken from a sample of 800 and
> samples 201-300 from a population of 300. The end result should look
> something like a heart rate monitor.

You could write your function to take a list of either subpopulations or sets 
of population parameters, lapply your simulation generator over the list and 
(assuming the output from each of those is a vector) use c(that.list, 
recursive=TRUE) to concatenate the resulting list of vectors into a single 
vector.


S Ellison


***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
R-help@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.


Re: [R] Nested for loop

2017-08-07 Thread Kirsten Morehouse
Hi Caitlin and Ben,

Thanks for your responses! My issue is that I'd like to create one
continuous line, rather than 3 lines overlayed.

The code I've attached works for a population of 400 and samples 100 times.
I'd like to extend this to 300 samples and 3 populations. So, the x-axis
would range from 0-300 samples.

What I'm having trouble with is finding a way to change the population
mid-way through the function. I want samples 1-100 to be taken from a
population of 400, samples 101-200 to be taken from a sample of 800 and
samples 201-300 from a population of 300. The end result should look
something like a heart rate monitor.

Aside from the rationale, does what I'm explaining make sense?

Best,

Kirsten

On Mon, Aug 7, 2017 at 3:18 PM, Caitlin  wrote:

> Hi.
>
> A nested for loop is not terribly efficient (it's O(n^2)). Can you
> vectorize it? If so, this would be a far more efficient and faster approach.
>
> ~Caitlin
>
> On Saturday, August 5, 2017, Kirsten Morehouse 
> wrote:
>
>> Hi! Thanks for taking the time to read this.
>>
>> The code below creates a graph that takes 100 samples that are between 5%
>> and 15% of the population (400).
>>
>> What I'd like to do, however, is add two other sections to the graph. It
>> would look something like this:
>>
>> from 1-100 samples take 100 samples that are between 5% and 15% of the
>> population (400). From 101-200 take 100 samples that are between 5% and
>> 15%
>> of the population (800). From 201-300 take 100 samples that are between 5%
>> and 15% of the population (300).
>>
>> I assume this would require a nested for loop. Does anyone have advice as
>> to how to do this?
>>
>> Thanks for your time. Kirsten
>>
>> ## Mark-Recapture
>> ## Estimate popoulation from repeated sampling
>>
>> ## Population size
>> N <- 400
>> N
>>
>> ## Vector labeling each item in the population
>> pop <- c(1:N)
>> pop
>>
>> ## Lower and upper bounds of sample size
>> lower.bound <- round(x = .05 * N, digits = 0)
>> lower.bound ## Smallest possible sample size
>>
>> upper.bound <- round(x = .15 * N, digits = 0)
>> upper.bound ## Largest possible sample size
>>
>> ## Length of sample size interval
>> length.ss.interval <- length(c(lower.bound:upper.bound))
>> length.ss.interval ## total possible sample sizes, ranging form
>> lower.bound
>> to upper.bound
>>
>> ## Determine a sample size randomly (not a global variable...simply for
>> test purposes)
>> ## Between lower and upper bounds set previously
>> ## Give equal weight to each possible sample size in this interval
>> sample(x = c(lower.bound:upper.bound),
>>size = 1,
>>prob = c(rep(1/length.ss.interval, length.ss.interval)))
>>
>> ## Specify number of samples to take
>> n.samples <- 100
>>
>> ## Initiate empty matrix
>> ## 1st column is population (item 1 thorugh item 400)
>> ## 2nd through nth column are all rounds of sampling
>> dat <- matrix(data = NA,
>>   nrow = length(pop),
>>   ncol = n.samples + 1)
>>
>> dat[,1] <- pop
>>
>> dat
>>
>> ## Take samples of random sizes
>> ## Record results in columns 2 through n
>> ## 1 = sampled (marked)
>> ## 0 = not sampled (not marked)
>> for(i in 2:ncol(dat)) {
>>   a.sample <- sample(x = pop,
>>  size = sample(x = c(lower.bound:upper.bound),
>>size = 1,
>>prob = c(rep(1/length.ss.interval,
>> length.ss.interval))),
>>  replace = FALSE)
>>   dat[,i] <- dat[,1] %in% a.sample
>> }
>>
>> ## How large was each sample size?
>> apply(X = dat, MARGIN = 2, FUN = sum)
>> ## 1st element is irrelevant
>> ## 2nd element through nth element: sample size for each of the 100
>> samples
>>
>> ## At this point, all computations can be done using dat
>>
>> ## Create Schnabel dataframe using dat
>> ## Google the Schnabel formula
>>
>> schnabel.comp <- data.frame(sample = 1:n.samples,
>> n.sampled = apply(X = dat, MARGIN = 2, FUN =
>> sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))]
>> )
>>
>> ## First column: which sample, 1-100
>> ## Second column: number selected in that sample
>>
>>
>> ## How many items were previously sampled?
>> ## For 1st sample, it's 0
>> ## For 2nd sample, code is different than for remaning samples
>>
>> n.prev.sampled <- c(0, rep(NA, n.samples-1))
>> n.prev.sampled
>>
>> n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1,
>> yes = 1,
>> no = 0))
>>
>> n.prev.sampled
>>
>> for(i in 4:ncol(dat)) {
>>   n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 &
>> rowSums(dat[,2:(i-1)]) > 0,
>> yes = 1,
>> no = 0))
>> }
>>
>> schnabel.comp$n.prev.sampled <- n.prev.sampled
>>
>> ## n.newly.sampled: in each sample, how many items were newly sampled?
>> ## i.e., never seen before?
>> schnabel.comp$n.newly.sampled <- with(schnabel.comp,
>>

Re: [R] Nested for loop

2017-08-07 Thread Ben Tupper
Hmmm.

If I understand you correctly, your question has to do with adding lines to 
your graph?  If so, my ggplot2 skills are sort of floppy, but you could append 
your sampling results to your data frame (one for each sample set) and then 
simply add layers.  Sort of like this.

N <- 10
x <- 1:N
df <- data.frame(
x = x,
y1 = sample(x, N, replace = TRUE),
y2 = sample(x, N, replace = TRUE),
y3 = sample(x, N, replace = TRUE))

ggplot(df, mapping = aes(x = x, y = y1)) +
geom_point(aes(y = y1), col = 'orange') + geom_line(aes(y = y1),col = 
'orange') +
geom_point(aes(y = y2), col = 'blue') + geom_line(aes(y = y2), col = 
'blue') +
geom_point(aes(y = y3), col = 'gray') + geom_line(aes(y = y3), col = 
'gray')

If plotting is not the issue then I don't understand what your question is.

Cheers,
Ben


> On Aug 6, 2017, at 3:44 PM, Kirsten Morehouse  wrote:
> 
> Hi Ben,
> 
> That's exactly right! Except for each set it's the sample population that is 
> 400, 800 or 300. I want to take 3 samples, each of 100, where only the 
> population differs. I can do this separately, but I'm having trouble putting 
> them all on the same graph. 
> 
> I'd like to have sample on the x axis (1-300) and estimate on the y axis. I 
> want to show how population affects the estimates. 
> 
> Does this make more sense?
> 
> Thanks for your time!
> 
> Kirsten 
> On Sun, Aug 6, 2017 at 3:21 PM Ben Tupper  > wrote:
> Hi Kirsten,
> 
> 
> 
> I can run your example code but I can't quite follow your division of 
> sampling.  Can you restate the the task?  Below is what I think you are 
> asking for, but I have the feeling I may be off the mark.
> 
> 
> 
> 
> 
> Set A: 400 samples, draw 100 in range of 5 to 15
> 
> 
> 
> Set B: 800 samples, draw 100 in range of 5 to 15
> 
> 
> 
> Set C: 300 samples, draw 100 in range of 5 to 15
> 
> 
> 
> Ben
> 
> 
> 
> > On Aug 5, 2017, at 9:21 AM, Kirsten Morehouse  > > wrote:
> 
> >
> 
> > Hi! Thanks for taking the time to read this.
> 
> >
> 
> > The code below creates a graph that takes 100 samples that are between 5%
> 
> > and 15% of the population (400).
> 
> >
> 
> > What I'd like to do, however, is add two other sections to the graph. It
> 
> > would look something like this:
> 
> >
> 
> > from 1-100 samples take 100 samples that are between 5% and 15% of the
> 
> > population (400). From 101-200 take 100 samples that are between 5% and 15%
> 
> > of the population (800). From 201-300 take 100 samples that are between 5%
> 
> > and 15% of the population (300).
> 
> >
> 
> > I assume this would require a nested for loop. Does anyone have advice as
> 
> > to how to do this?
> 
> >
> 
> > Thanks for your time. Kirsten
> 
> >
> 
> > ## Mark-Recapture
> 
> > ## Estimate popoulation from repeated sampling
> 
> >
> 
> > ## Population size
> 
> > N <- 400
> 
> > N
> 
> >
> 
> > ## Vector labeling each item in the population
> 
> > pop <- c(1:N)
> 
> > pop
> 
> >
> 
> > ## Lower and upper bounds of sample size
> 
> > lower.bound <- round(x = .05 * N, digits = 0)
> 
> > lower.bound ## Smallest possible sample size
> 
> >
> 
> > upper.bound <- round(x = .15 * N, digits = 0)
> 
> > upper.bound ## Largest possible sample size
> 
> >
> 
> > ## Length of sample size interval
> 
> > length.ss.interval <- length(c(lower.bound:upper.bound))
> 
> > length.ss.interval ## total possible sample sizes, ranging form lower.bound
> 
> > to upper.bound
> 
> >
> 
> > ## Determine a sample size randomly (not a global variable...simply for
> 
> > test purposes)
> 
> > ## Between lower and upper bounds set previously
> 
> > ## Give equal weight to each possible sample size in this interval
> 
> > sample(x = c(lower.bound:upper.bound),
> 
> >   size = 1,
> 
> >   prob = c(rep(1/length.ss.interval, length.ss.interval)))
> 
> >
> 
> > ## Specify number of samples to take
> 
> > n.samples <- 100
> 
> >
> 
> > ## Initiate empty matrix
> 
> > ## 1st column is population (item 1 thorugh item 400)
> 
> > ## 2nd through nth column are all rounds of sampling
> 
> > dat <- matrix(data = NA,
> 
> >  nrow = length(pop),
> 
> >  ncol = n.samples + 1)
> 
> >
> 
> > dat[,1] <- pop
> 
> >
> 
> > dat
> 
> >
> 
> > ## Take samples of random sizes
> 
> > ## Record results in columns 2 through n
> 
> > ## 1 = sampled (marked)
> 
> > ## 0 = not sampled (not marked)
> 
> > for(i in 2:ncol(dat)) {
> 
> >  a.sample <- sample(x = pop,
> 
> > size = sample(x = c(lower.bound:upper.bound),
> 
> >   size = 1,
> 
> >   prob = c(rep(1/length.ss.interval,
> 
> > length.ss.interval))),
> 
> > replace = FALSE)
> 
> >  dat[,i] <- dat[,1] %in% a.sample
> 
> > }
> 
> >
> 
> > ## How large was each sample size?
> 
> > apply(X = dat, MARGIN = 2, FUN = sum)
> 
> > ## 1st element is irrelevant
> 
> > ## 2nd element t

Re: [R] Nested for loop

2017-08-06 Thread Kirsten Morehouse
Hi Ben,

That's exactly right! Except for each set it's the sample population that
is 400, 800 or 300. I want to take 3 samples, each of 100, where only the
population differs. I can do this separately, but I'm having trouble
putting them all on the same graph.

I'd like to have sample on the x axis (1-300) and estimate on the y axis. I
want to show how population affects the estimates.

Does this make more sense?

Thanks for your time!

Kirsten
On Sun, Aug 6, 2017 at 3:21 PM Ben Tupper  wrote:

> Hi Kirsten,
>
>
>
> I can run your example code but I can't quite follow your division of
> sampling.  Can you restate the the task?  Below is what I think you are
> asking for, but I have the feeling I may be off the mark.
>
>
>
>
>
> Set A: 400 samples, draw 100 in range of 5 to 15
>
>
>
> Set B: 800 samples, draw 100 in range of 5 to 15
>
>
>
> Set C: 300 samples, draw 100 in range of 5 to 15
>
>
>
> Ben
>
>
>
> > On Aug 5, 2017, at 9:21 AM, Kirsten Morehouse 
> wrote:
>
> >
>
> > Hi! Thanks for taking the time to read this.
>
> >
>
> > The code below creates a graph that takes 100 samples that are between 5%
>
> > and 15% of the population (400).
>
> >
>
> > What I'd like to do, however, is add two other sections to the graph. It
>
> > would look something like this:
>
> >
>
> > from 1-100 samples take 100 samples that are between 5% and 15% of the
>
> > population (400). From 101-200 take 100 samples that are between 5% and
> 15%
>
> > of the population (800). From 201-300 take 100 samples that are between
> 5%
>
> > and 15% of the population (300).
>
> >
>
> > I assume this would require a nested for loop. Does anyone have advice as
>
> > to how to do this?
>
> >
>
> > Thanks for your time. Kirsten
>
> >
>
> > ## Mark-Recapture
>
> > ## Estimate popoulation from repeated sampling
>
> >
>
> > ## Population size
>
> > N <- 400
>
> > N
>
> >
>
> > ## Vector labeling each item in the population
>
> > pop <- c(1:N)
>
> > pop
>
> >
>
> > ## Lower and upper bounds of sample size
>
> > lower.bound <- round(x = .05 * N, digits = 0)
>
> > lower.bound ## Smallest possible sample size
>
> >
>
> > upper.bound <- round(x = .15 * N, digits = 0)
>
> > upper.bound ## Largest possible sample size
>
> >
>
> > ## Length of sample size interval
>
> > length.ss.interval <- length(c(lower.bound:upper.bound))
>
> > length.ss.interval ## total possible sample sizes, ranging form
> lower.bound
>
> > to upper.bound
>
> >
>
> > ## Determine a sample size randomly (not a global variable...simply for
>
> > test purposes)
>
> > ## Between lower and upper bounds set previously
>
> > ## Give equal weight to each possible sample size in this interval
>
> > sample(x = c(lower.bound:upper.bound),
>
> >   size = 1,
>
> >   prob = c(rep(1/length.ss.interval, length.ss.interval)))
>
> >
>
> > ## Specify number of samples to take
>
> > n.samples <- 100
>
> >
>
> > ## Initiate empty matrix
>
> > ## 1st column is population (item 1 thorugh item 400)
>
> > ## 2nd through nth column are all rounds of sampling
>
> > dat <- matrix(data = NA,
>
> >  nrow = length(pop),
>
> >  ncol = n.samples + 1)
>
> >
>
> > dat[,1] <- pop
>
> >
>
> > dat
>
> >
>
> > ## Take samples of random sizes
>
> > ## Record results in columns 2 through n
>
> > ## 1 = sampled (marked)
>
> > ## 0 = not sampled (not marked)
>
> > for(i in 2:ncol(dat)) {
>
> >  a.sample <- sample(x = pop,
>
> > size = sample(x = c(lower.bound:upper.bound),
>
> >   size = 1,
>
> >   prob = c(rep(1/length.ss.interval,
>
> > length.ss.interval))),
>
> > replace = FALSE)
>
> >  dat[,i] <- dat[,1] %in% a.sample
>
> > }
>
> >
>
> > ## How large was each sample size?
>
> > apply(X = dat, MARGIN = 2, FUN = sum)
>
> > ## 1st element is irrelevant
>
> > ## 2nd element through nth element: sample size for each of the 100
> samples
>
> >
>
> > ## At this point, all computations can be done using dat
>
> >
>
> > ## Create Schnabel dataframe using dat
>
> > ## Google the Schnabel formula
>
> >
>
> > schnabel.comp <- data.frame(sample = 1:n.samples,
>
> >n.sampled = apply(X = dat, MARGIN = 2, FUN =
>
> > sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))]
>
> > )
>
> >
>
> > ## First column: which sample, 1-100
>
> > ## Second column: number selected in that sample
>
> >
>
> >
>
> > ## How many items were previously sampled?
>
> > ## For 1st sample, it's 0
>
> > ## For 2nd sample, code is different than for remaning samples
>
> >
>
> > n.prev.sampled <- c(0, rep(NA, n.samples-1))
>
> > n.prev.sampled
>
> >
>
> > n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1,
>
> >yes = 1,
>
> >no = 0))
>
> >
>
> > n.prev.sampled
>
> >
>
> > for(i in 4:ncol(dat)) {
>
> >  n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 &
>
> > rowSums(dat[,2:(i-1)]) > 0,
>
> >

Re: [R] Nested for loop

2017-08-06 Thread Ben Tupper
Hi Kirsten,

I can run your example code but I can't quite follow your division of sampling. 
 Can you restate the the task?  Below is what I think you are asking for, but I 
have the feeling I may be off the mark.


Set A: 400 samples, draw 100 in range of 5 to 15

Set B: 800 samples, draw 100 in range of 5 to 15

Set C: 300 samples, draw 100 in range of 5 to 15

Ben

> On Aug 5, 2017, at 9:21 AM, Kirsten Morehouse  wrote:
> 
> Hi! Thanks for taking the time to read this.
> 
> The code below creates a graph that takes 100 samples that are between 5%
> and 15% of the population (400).
> 
> What I'd like to do, however, is add two other sections to the graph. It
> would look something like this:
> 
> from 1-100 samples take 100 samples that are between 5% and 15% of the
> population (400). From 101-200 take 100 samples that are between 5% and 15%
> of the population (800). From 201-300 take 100 samples that are between 5%
> and 15% of the population (300).
> 
> I assume this would require a nested for loop. Does anyone have advice as
> to how to do this?
> 
> Thanks for your time. Kirsten
> 
> ## Mark-Recapture
> ## Estimate popoulation from repeated sampling
> 
> ## Population size
> N <- 400
> N
> 
> ## Vector labeling each item in the population
> pop <- c(1:N)
> pop
> 
> ## Lower and upper bounds of sample size
> lower.bound <- round(x = .05 * N, digits = 0)
> lower.bound ## Smallest possible sample size
> 
> upper.bound <- round(x = .15 * N, digits = 0)
> upper.bound ## Largest possible sample size
> 
> ## Length of sample size interval
> length.ss.interval <- length(c(lower.bound:upper.bound))
> length.ss.interval ## total possible sample sizes, ranging form lower.bound
> to upper.bound
> 
> ## Determine a sample size randomly (not a global variable...simply for
> test purposes)
> ## Between lower and upper bounds set previously
> ## Give equal weight to each possible sample size in this interval
> sample(x = c(lower.bound:upper.bound),
>   size = 1,
>   prob = c(rep(1/length.ss.interval, length.ss.interval)))
> 
> ## Specify number of samples to take
> n.samples <- 100
> 
> ## Initiate empty matrix
> ## 1st column is population (item 1 thorugh item 400)
> ## 2nd through nth column are all rounds of sampling
> dat <- matrix(data = NA,
>  nrow = length(pop),
>  ncol = n.samples + 1)
> 
> dat[,1] <- pop
> 
> dat
> 
> ## Take samples of random sizes
> ## Record results in columns 2 through n
> ## 1 = sampled (marked)
> ## 0 = not sampled (not marked)
> for(i in 2:ncol(dat)) {
>  a.sample <- sample(x = pop,
> size = sample(x = c(lower.bound:upper.bound),
>   size = 1,
>   prob = c(rep(1/length.ss.interval,
> length.ss.interval))),
> replace = FALSE)
>  dat[,i] <- dat[,1] %in% a.sample
> }
> 
> ## How large was each sample size?
> apply(X = dat, MARGIN = 2, FUN = sum)
> ## 1st element is irrelevant
> ## 2nd element through nth element: sample size for each of the 100 samples
> 
> ## At this point, all computations can be done using dat
> 
> ## Create Schnabel dataframe using dat
> ## Google the Schnabel formula
> 
> schnabel.comp <- data.frame(sample = 1:n.samples,
>n.sampled = apply(X = dat, MARGIN = 2, FUN =
> sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))]
> )
> 
> ## First column: which sample, 1-100
> ## Second column: number selected in that sample
> 
> 
> ## How many items were previously sampled?
> ## For 1st sample, it's 0
> ## For 2nd sample, code is different than for remaning samples
> 
> n.prev.sampled <- c(0, rep(NA, n.samples-1))
> n.prev.sampled
> 
> n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1,
>yes = 1,
>no = 0))
> 
> n.prev.sampled
> 
> for(i in 4:ncol(dat)) {
>  n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 &
> rowSums(dat[,2:(i-1)]) > 0,
>yes = 1,
>no = 0))
> }
> 
> schnabel.comp$n.prev.sampled <- n.prev.sampled
> 
> ## n.newly.sampled: in each sample, how many items were newly sampled?
> ## i.e., never seen before?
> schnabel.comp$n.newly.sampled <- with(schnabel.comp,
>  n.sampled - n.prev.sampled)
> 
> ## cum.sampled: how many total items have you seen?
> schnabel.comp$cum.sampled <- c(0,
> cumsum(schnabel.comp$n.newly.sampled)[2:n.samples-1])
> 
> ## numerator of schnabel formula
> schnabel.comp$numerator <- with(schnabel.comp,
>n.sampled * cum.sampled)
> 
> ## denominator of schnable formula is n.prev.sampled
> 
> ## pop.estimate -- after each sample (starting with 2nd -- need at least
> two samples)
> schnabel.comp$pop.estimate <- NA
> 
> for(i in 1:length(schnabel.comp$pop.estimate)) {
>  schnabel.comp$pop.estimate[i] <- sum(schnabel.comp$numerator[1:i]) /
> sum(schnabel.comp$n.prev

[R] Nested for loop

2017-08-05 Thread Kirsten Morehouse
Hi! Thanks for taking the time to read this.

The code below creates a graph that takes 100 samples that are between 5%
and 15% of the population (400).

What I'd like to do, however, is add two other sections to the graph. It
would look something like this:

from 1-100 samples take 100 samples that are between 5% and 15% of the
population (400). From 101-200 take 100 samples that are between 5% and 15%
of the population (800). From 201-300 take 100 samples that are between 5%
and 15% of the population (300).

I assume this would require a nested for loop. Does anyone have advice as
to how to do this?

Thanks for your time. Kirsten

## Mark-Recapture
## Estimate popoulation from repeated sampling

## Population size
N <- 400
N

## Vector labeling each item in the population
pop <- c(1:N)
pop

## Lower and upper bounds of sample size
lower.bound <- round(x = .05 * N, digits = 0)
lower.bound ## Smallest possible sample size

upper.bound <- round(x = .15 * N, digits = 0)
upper.bound ## Largest possible sample size

## Length of sample size interval
length.ss.interval <- length(c(lower.bound:upper.bound))
length.ss.interval ## total possible sample sizes, ranging form lower.bound
to upper.bound

## Determine a sample size randomly (not a global variable...simply for
test purposes)
## Between lower and upper bounds set previously
## Give equal weight to each possible sample size in this interval
sample(x = c(lower.bound:upper.bound),
   size = 1,
   prob = c(rep(1/length.ss.interval, length.ss.interval)))

## Specify number of samples to take
n.samples <- 100

## Initiate empty matrix
## 1st column is population (item 1 thorugh item 400)
## 2nd through nth column are all rounds of sampling
dat <- matrix(data = NA,
  nrow = length(pop),
  ncol = n.samples + 1)

dat[,1] <- pop

dat

## Take samples of random sizes
## Record results in columns 2 through n
## 1 = sampled (marked)
## 0 = not sampled (not marked)
for(i in 2:ncol(dat)) {
  a.sample <- sample(x = pop,
 size = sample(x = c(lower.bound:upper.bound),
   size = 1,
   prob = c(rep(1/length.ss.interval,
length.ss.interval))),
 replace = FALSE)
  dat[,i] <- dat[,1] %in% a.sample
}

## How large was each sample size?
apply(X = dat, MARGIN = 2, FUN = sum)
## 1st element is irrelevant
## 2nd element through nth element: sample size for each of the 100 samples

## At this point, all computations can be done using dat

## Create Schnabel dataframe using dat
## Google the Schnabel formula

schnabel.comp <- data.frame(sample = 1:n.samples,
n.sampled = apply(X = dat, MARGIN = 2, FUN =
sum)[2:length(apply(X = dat, MARGIN = 2, FUN = sum))]
)

## First column: which sample, 1-100
## Second column: number selected in that sample


## How many items were previously sampled?
## For 1st sample, it's 0
## For 2nd sample, code is different than for remaning samples

n.prev.sampled <- c(0, rep(NA, n.samples-1))
n.prev.sampled

n.prev.sampled[2] <- sum(ifelse(test = dat[,3] == 1 & dat[,2] == 1,
yes = 1,
no = 0))

n.prev.sampled

for(i in 4:ncol(dat)) {
  n.prev.sampled[i-1] <- sum(ifelse(test = dat[,i] == 1 &
rowSums(dat[,2:(i-1)]) > 0,
yes = 1,
no = 0))
}

schnabel.comp$n.prev.sampled <- n.prev.sampled

## n.newly.sampled: in each sample, how many items were newly sampled?
## i.e., never seen before?
schnabel.comp$n.newly.sampled <- with(schnabel.comp,
  n.sampled - n.prev.sampled)

## cum.sampled: how many total items have you seen?
schnabel.comp$cum.sampled <- c(0,
cumsum(schnabel.comp$n.newly.sampled)[2:n.samples-1])

## numerator of schnabel formula
schnabel.comp$numerator <- with(schnabel.comp,
n.sampled * cum.sampled)

## denominator of schnable formula is n.prev.sampled

## pop.estimate -- after each sample (starting with 2nd -- need at least
two samples)
schnabel.comp$pop.estimate <- NA

for(i in 1:length(schnabel.comp$pop.estimate)) {
  schnabel.comp$pop.estimate[i] <- sum(schnabel.comp$numerator[1:i]) /
sum(schnabel.comp$n.prev.sampled[1:i])
}


## Plot population estimate after each sample
if (!require("ggplot2")) {install.packages("ggplot2"); require("ggplot2")}
if (!require("scales")) {install.packages("scales"); require("scales")}


small.sample.dat <- schnabel.comp

small.sample <- ggplot(data = small.sample.dat,
   mapping = aes(x = sample, y = pop.estimate)) +
  geom_point(size = 2) +
  geom_line() +
  geom_hline(yintercept = N, col = "red", lwd = 1) +
  coord_cartesian(xlim = c(0:100), ylim = c(300:500)) +
  scale_x_continuous(breaks = pretty_breaks(11)) +
  scale_y_continuous(breaks = pretty_breaks(11)) +
  labs(x = "\nSample", y = "Population estimate\n",
   title = "Sampl

Re: [R] nested for loop with data table

2017-05-06 Thread Ek Esawi
Thank you Jeff. Your idea, as i mentioned on my previous posting, did
indeed work. I read somewhere that both data table dplyr do great many
things and i plan to learn both as much as i can. Suggestions on this list
either get you the answer you are looking for or give you lead to an answer.

Thanks again

On Thu, May 4, 2017 at 12:04 AM, Jeff Newmiller 
wrote:

> You seem to be unaware of the "aggregate" data processing concept. There
> are many ways to accomplish aggregation, but I am not fluent in data.table
> methods but knowing the concept is the first step.
>
> Perhaps look closely at [1], or Google for data table aggregation yourself?
>
> [1] https://www.r-bloggers.com/efficient-aggregation-and-
> more-using-data-table/amp/
> --
> Sent from my phone. Please excuse my brevity.
>
> On May 3, 2017 8:17:21 AM PDT, Ek Esawi  wrote:
> >Thank you both Boris and Jim. Thank you, Boris, for advising to read
> >the
> >posting guide; I had and I just did.
> >
> >Jim’s idea is exactly what I want; however, I could not pass sset1,
> >sset2,
> >etc. to the j nested loop and collect the results in an vector.
> >
> >Here attached my code, file, and my question which should be clear now.
> >The
> >question again is instead of using separate loops for each sset1 and
> >sset2,
> >I want one nested loop? Because I have at least 10 subsets
> >(sset1,sset2,sset3…..sset10).
> >
> >Thanks again, EK
> >
> >
> >---The code--
> >
> >install.packages("data.table")
> >library(data.table)
> >File1 <-  "C:/Users/SampleData.csv"
> >DT <- fread(File1)
> >sset1 <- DT[Num<10&Day<10]
> >sset2 <- DT[Num>10&Day<15]
> >
> ># Count how many combinations of A,B,C,D,E,F in each subset
> >for ( i in 1:length(sset1)){
> >  aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
> >  bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
> >  cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
> >  counts <- c(aa, bb,cc)
> >}
> >
> >for ( i in 1:length(sset2)){
> >  aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
> >  bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
> >  cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
> >  counts <-  c(aa1,bb1,cc1)
> >}
> >
> >---The File
> >
> >   Num  Color Grade ValueMonth Day
> > 1:   1 yellow A20  May   1
> > 2:   2  green B25 June   2
> > 3:   3  green A10April   3
> > 4:   4  black A17   August   3
> > 5:   5red C 5 December   5
> > 6:   6 orange D 0  January  13
> > 7:   7 orange E12  January   5
> > 8:   8 orange F11 February   8
> > 9:   9 orange F99 July  23
> >10:  10 orange F70  May   7
> >11:  11  black A77 June  11
> >12:  12  green B87April  33
> >13:  13  black A79   August   9
> >14:  14  green A68 December  14
> >15:  15  black C90  January  31
> >16:  16  green D79  January  11
> >17:  17  black E   101 February  17
> >18:  18red F90 July  21
> >19:  19red F   112 February  13
> >20:  20red F   101 July  20
> >
> >On Tue, May 2, 2017 at 12:35 PM, Ek Esawi  wrote:
> >
> >> I have a huge data file; a sample is listed below. I am using the
> >package
> >> data table to process the file and I am stuck on one issue and need
> >some
> >> feedback. I used fread to create a data table. Then I divided the
> >data
> >> table (named File1) into 10 general subsets using common table
> >commands
> >> such as:
> >>
> >>
> >>
> >> AAA <- File1[Num<5&day>15]
> >>
> >> BBB <- File1[Num>15&day<10]
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >> …..
> >>
> >>
> >>
> >> I wanted to divide and count each of the above subsets based on a set
> >of
> >> parameters common to all subsets. I did the following to go through
> >each
> >> subset and it works:
> >>
> >> For (I in 1: length (AAA)) {
> >>
> >>   aa <- c(AAA[color==”green”&grade==”a”,month==”Januray”
> >> .N],[ AAA[color==”green”&grade==”b”& month==”June”’ .N])
> >>
> >> }
> >>
> >>
> >>
> >> The question: I don’t want to have a separate loop for each subset
> >(10
> >> loops). Instead, I was hoping to have 2 nested loops in the form
> >below:
> >>
> >>
> >>
> >> For (I in 1:N)){
> >>
> >>   For (j in 1:M){
> >>
> >>
> >>
> >> }
> >>
> >> }
> >>
> >>
> >>
> >>  Sample
> >>
> >>
> >> Num
> >>
> >> Color
> >>
> >> Grade
> >>
> >> Value
> >>
> >> Month
> >>
> >> Day
> >>
> >> 1
> >>
> >> yellow
> >>
> >> A
> >>
> >> 20
> >>
> >> May
> >>
> >> 1
> >>
> >> 2
> >>
> >> green
> >>
> >> B
> >>
> >> 25
> >>
> >> June
> >>
> >> 2
> >>
> >> 3
> >>
> >> green
> >>
> >> A
> >>
> >> 10
> >>
> >> April
> >>
> >> 3
> >>
> >> 4
> >>
> >> black
> >>
> >> A
> >>
> >> 17
> >>
> >> August
> >>
> >> 3
> >>
> >> 5
> >>
> >> red
> >>
> >> C
> >>
> >> 5
> >>
> >> December
> >>
> >> 5
> >>
> >> 6
> >>
> >> orange
> >>
> >> D
> >>
> >> 0
> >>
> >> January
> >>
> >> 13
> >>
> >> 7
> >>
> >> orange
> >>

Re: [R] nested for loop with data table

2017-05-03 Thread PIKAL Petr
Hi

better to present us your data by dput, so they can be directly used.

> dput(dat)
dat <- structure(list(Num = 1:20, Color = structure(c(5L, 2L, 2L, 1L,
4L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 4L, 4L, 4L
), .Label = c("black", "green", "orange", "red", "yellow"), class = "factor"),
Grade = structure(c(1L, 2L, 1L, 1L, 3L, 4L, 5L, 6L, 6L, 6L,
1L, 2L, 1L, 1L, 3L, 4L, 5L, 6L, 6L, 6L), .Label = c("A",
"B", "C", "D", "E", "F"), class = "factor"), value = c(20L,
25L, 10L, 17L, 5L, 0L, 12L, 11L, 99L, 70L, 77L, 87L, 79L,
68L, 90L, 79L, 101L, 90L, 112L, 101L), Month = structure(c(8L,
7L, 1L, 2L, 3L, 5L, 5L, 4L, 6L, 8L, 7L, 1L, 2L, 3L, 5L, 5L,
4L, 6L, 4L, 6L), .Label = c("April", "August", "December",
"February", "January", "July", "June", "May"), class = "factor"),
Day = c(1L, 2L, 3L, 3L, 5L, 13L, 5L, 8L, 23L, 7L, 11L, 33L,
9L, 14L, 31L, 11L, 17L, 21L, 13L, 20L)), .Names = c("Num",
"Color", "Grade", "value", "Month", "Day"), class = "data.frame", row.names = 
c(NA,
-20L))
>

I do not know your exact intention and data.table commands. You can get some 
summary numbers simply by

table(dat$Grade[dat$Num<10 & dat$Day<10])

A B C D E F
3 1 1 0 1 1

It is probably preferable to obtain logical vectors for Num and Day before 
starting tabulation.

Cheers
Petr


> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ek Esawi
> Sent: Wednesday, May 3, 2017 5:17 PM
> To: r-help@r-project.org
> Subject: Re: [R] nested for loop with data table
>
> Thank you both Boris and Jim. Thank you, Boris, for advising to read the
> posting guide; I had and I just did.
>
> Jim’s idea is exactly what I want; however, I could not pass sset1, sset2, 
> etc.
> to the j nested loop and collect the results in an vector.
>
> Here attached my code, file, and my question which should be clear now.
> The question again is instead of using separate loops for each sset1 and
> sset2, I want one nested loop? Because I have at least 10 subsets
> (sset1,sset2,sset3…..sset10).
>
> Thanks again, EK
>
>
> ---The code--
>
> install.packages("data.table")
> library(data.table)
> File1 <-  "C:/Users/SampleData.csv"
> DT <- fread(File1)
> sset1 <- DT[Num<10&Day<10]
> sset2 <- DT[Num>10&Day<15]
>
> # Count how many combinations of A,B,C,D,E,F in each subset for ( i in
> 1:length(sset1)){
>   aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
>   bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
>   cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
>   counts <- c(aa, bb,cc)
> }
>
> for ( i in 1:length(sset2)){
>   aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
>   bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
>   cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
>   counts <-  c(aa1,bb1,cc1)
> }
>
> ---The File
>
>Num  Color Grade ValueMonth Day
>  1:   1 yellow A20  May   1
>  2:   2  green B25 June   2
>  3:   3  green A10April   3
>  4:   4  black A17   August   3
>  5:   5red C 5 December   5
>  6:   6 orange D 0  January  13
>  7:   7 orange E12  January   5
>  8:   8 orange F11 February   8
>  9:   9 orange F99 July  23
> 10:  10 orange F70  May   7
> 11:  11  black A77 June  11
> 12:  12  green B87April  33
> 13:  13  black A79   August   9
> 14:  14  green A68 December  14
> 15:  15  black C90  January  31
> 16:  16  green D79  January  11
> 17:  17  black E   101 February  17
> 18:  18red F90 July  21
> 19:  19red F   112 February  13
> 20:  20red F   101 July  20
>
> On Tue, May 2, 2017 at 12:35 PM, Ek Esawi  wrote:
>
> > I have a huge data file; a sample is listed below. I am using the
> > package data table to process the file and I am stuck on one issue and
> > need some feedback. I used fread to create a data table. Then I
> > divided the data table (named File1) into 10 general subsets using
> > common table commands such as:
> >
> >
> >
> > AAA <- File1[Num<5&day>15]
> >
> > BBB <- File1[Num>15&day<10]
> &g

Re: [R] nested for loop with data table

2017-05-03 Thread Jeff Newmiller
You seem to be unaware of the "aggregate" data processing concept. There are 
many ways to accomplish aggregation, but I am not fluent in data.table methods 
but knowing the concept is the first step.

Perhaps look closely at [1], or Google for data table aggregation yourself? 

[1] 
https://www.r-bloggers.com/efficient-aggregation-and-more-using-data-table/amp/
-- 
Sent from my phone. Please excuse my brevity.

On May 3, 2017 8:17:21 AM PDT, Ek Esawi  wrote:
>Thank you both Boris and Jim. Thank you, Boris, for advising to read
>the
>posting guide; I had and I just did.
>
>Jim’s idea is exactly what I want; however, I could not pass sset1,
>sset2,
>etc. to the j nested loop and collect the results in an vector.
>
>Here attached my code, file, and my question which should be clear now.
>The
>question again is instead of using separate loops for each sset1 and
>sset2,
>I want one nested loop? Because I have at least 10 subsets
>(sset1,sset2,sset3…..sset10).
>
>Thanks again, EK
>
>
>---The code--
>
>install.packages("data.table")
>library(data.table)
>File1 <-  "C:/Users/SampleData.csv"
>DT <- fread(File1)
>sset1 <- DT[Num<10&Day<10]
>sset2 <- DT[Num>10&Day<15]
>
># Count how many combinations of A,B,C,D,E,F in each subset
>for ( i in 1:length(sset1)){
>  aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
>  bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
>  cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
>  counts <- c(aa, bb,cc)
>}
>
>for ( i in 1:length(sset2)){
>  aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
>  bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
>  cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
>  counts <-  c(aa1,bb1,cc1)
>}
>
>---The File
>
>   Num  Color Grade ValueMonth Day
> 1:   1 yellow A20  May   1
> 2:   2  green B25 June   2
> 3:   3  green A10April   3
> 4:   4  black A17   August   3
> 5:   5red C 5 December   5
> 6:   6 orange D 0  January  13
> 7:   7 orange E12  January   5
> 8:   8 orange F11 February   8
> 9:   9 orange F99 July  23
>10:  10 orange F70  May   7
>11:  11  black A77 June  11
>12:  12  green B87April  33
>13:  13  black A79   August   9
>14:  14  green A68 December  14
>15:  15  black C90  January  31
>16:  16  green D79  January  11
>17:  17  black E   101 February  17
>18:  18red F90 July  21
>19:  19red F   112 February  13
>20:  20red F   101 July  20
>
>On Tue, May 2, 2017 at 12:35 PM, Ek Esawi  wrote:
>
>> I have a huge data file; a sample is listed below. I am using the
>package
>> data table to process the file and I am stuck on one issue and need
>some
>> feedback. I used fread to create a data table. Then I divided the
>data
>> table (named File1) into 10 general subsets using common table
>commands
>> such as:
>>
>>
>>
>> AAA <- File1[Num<5&day>15]
>>
>> BBB <- File1[Num>15&day<10]
>>
>> …..
>>
>> …..
>>
>> …..
>>
>> …..
>>
>> …..
>>
>> …..
>>
>>
>>
>> I wanted to divide and count each of the above subsets based on a set
>of
>> parameters common to all subsets. I did the following to go through
>each
>> subset and it works:
>>
>> For (I in 1: length (AAA)) {
>>
>>   aa <- c(AAA[color==”green”&grade==”a”,month==”Januray”
>> .N],[ AAA[color==”green”&grade==”b”& month==”June”’ .N])
>>
>> }
>>
>>
>>
>> The question: I don’t want to have a separate loop for each subset
>(10
>> loops). Instead, I was hoping to have 2 nested loops in the form
>below:
>>
>>
>>
>> For (I in 1:N)){
>>
>>   For (j in 1:M){
>>
>>
>>
>> }
>>
>> }
>>
>>
>>
>>  Sample
>>
>>
>> Num
>>
>> Color
>>
>> Grade
>>
>> Value
>>
>> Month
>>
>> Day
>>
>> 1
>>
>> yellow
>>
>> A
>>
>> 20
>>
>> May
>>
>> 1
>>
>> 2
>>
>> green
>>
>> B
>>
>> 25
>>
>> June
>>
>> 2
>>
>> 3
>>
>> green
>>
>> A
>>
>> 10
>>
>> April
>>
>> 3
>>
>> 4
>>
>> black
>>
>> A
>>
>> 17
>>
>> August
>>
>> 3
>>
>> 5
>>
>> red
>>
>> C
>>
>> 5
>>
>> December
>>
>> 5
>>
>> 6
>>
>> orange
>>
>> D
>>
>> 0
>>
>> January
>>
>> 13
>>
>> 7
>>
>> orange
>>
>> E
>>
>> 12
>>
>> January
>>
>> 5
>>
>> 8
>>
>> orange
>>
>> F
>>
>> 11
>>
>> February
>>
>> 8
>>
>> 9
>>
>> orange
>>
>> F
>>
>> 99
>>
>> July
>>
>> 23
>>
>> 10
>>
>> orange
>>
>> F
>>
>> 70
>>
>> May
>>
>> 7
>>
>> 11
>>
>> black
>>
>> A
>>
>> 77
>>
>> June
>>
>> 11
>>
>> 12
>>
>> green
>>
>> B
>>
>> 87
>>
>> April
>>
>> 33
>>
>> 13
>>
>> black
>>
>> A
>>
>> 79
>>
>> August
>>
>> 9
>>
>> 14
>>
>> green
>>
>> A
>>
>> 68
>>
>> December
>>
>> 14
>>
>> 15
>>
>> black
>>
>> C
>>
>> 90
>>
>> January
>>
>> 31
>>
>> 16
>>
>> green
>>
>> D
>>
>> 79
>>
>> January
>>
>> 11
>>
>> 17
>>
>> black
>>
>> E
>>
>> 101
>>
>> February
>>
>> 17
>>
>> 18
>>
>> red
>>
>> F
>>
>> 90
>>
>> July
>>
>> 21
>>
>> 19
>>
>> red
>>
>> F
>>
>> 112
>>
>> February
>>
>> 13
>>
>> 20
>>
>> red
>>
>> F
>>
>> 101
>>
>> July
>>
>> 20
>>
>>
>>
>
>  

Re: [R] nested for loop with data table

2017-05-03 Thread Ek Esawi
Thank you both Boris and Jim. Thank you, Boris, for advising to read the
posting guide; I had and I just did.

Jim’s idea is exactly what I want; however, I could not pass sset1, sset2,
etc. to the j nested loop and collect the results in an vector.

Here attached my code, file, and my question which should be clear now. The
question again is instead of using separate loops for each sset1 and sset2,
I want one nested loop? Because I have at least 10 subsets
(sset1,sset2,sset3…..sset10).

Thanks again, EK


---The code--

install.packages("data.table")
library(data.table)
File1 <-  "C:/Users/SampleData.csv"
DT <- fread(File1)
sset1 <- DT[Num<10&Day<10]
sset2 <- DT[Num>10&Day<15]

# Count how many combinations of A,B,C,D,E,F in each subset
for ( i in 1:length(sset1)){
  aa <- c(sset1[Grade=="A",.N],sset1[Grade=="D",.N])
  bb <- c(sset1[Grade=="B",.N],sset1[Grade=="F",.N])
  cc <- c(sset1[Grade=="C",.N],sset1[Grade=="A",.N])
  counts <- c(aa, bb,cc)
}

for ( i in 1:length(sset2)){
  aa1 <- c(sset2[Grade=="A",.N],sset2[Grade=="D",.N])
  bb1 <- c(sset2[Grade=="B",.N],sset2[Grade=="F",.N])
  cc1 <- c(sset2[Grade=="C",.N],sset2[Grade=="A",.N])
  counts <-  c(aa1,bb1,cc1)
}

---The File

   Num  Color Grade ValueMonth Day
 1:   1 yellow A20  May   1
 2:   2  green B25 June   2
 3:   3  green A10April   3
 4:   4  black A17   August   3
 5:   5red C 5 December   5
 6:   6 orange D 0  January  13
 7:   7 orange E12  January   5
 8:   8 orange F11 February   8
 9:   9 orange F99 July  23
10:  10 orange F70  May   7
11:  11  black A77 June  11
12:  12  green B87April  33
13:  13  black A79   August   9
14:  14  green A68 December  14
15:  15  black C90  January  31
16:  16  green D79  January  11
17:  17  black E   101 February  17
18:  18red F90 July  21
19:  19red F   112 February  13
20:  20red F   101 July  20

On Tue, May 2, 2017 at 12:35 PM, Ek Esawi  wrote:

> I have a huge data file; a sample is listed below. I am using the package
> data table to process the file and I am stuck on one issue and need some
> feedback. I used fread to create a data table. Then I divided the data
> table (named File1) into 10 general subsets using common table commands
> such as:
>
>
>
> AAA <- File1[Num<5&day>15]
>
> BBB <- File1[Num>15&day<10]
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
>
>
> I wanted to divide and count each of the above subsets based on a set of
> parameters common to all subsets. I did the following to go through each
> subset and it works:
>
> For (I in 1: length (AAA)) {
>
>   aa <- c(AAA[color==”green”&grade==”a”,month==”Januray”
> .N],[ AAA[color==”green”&grade==”b”& month==”June”’ .N])
>
> }
>
>
>
> The question: I don’t want to have a separate loop for each subset (10
> loops). Instead, I was hoping to have 2 nested loops in the form below:
>
>
>
> For (I in 1:N)){
>
>   For (j in 1:M){
>
>
>
> }
>
> }
>
>
>
>  Sample
>
>
> Num
>
> Color
>
> Grade
>
> Value
>
> Month
>
> Day
>
> 1
>
> yellow
>
> A
>
> 20
>
> May
>
> 1
>
> 2
>
> green
>
> B
>
> 25
>
> June
>
> 2
>
> 3
>
> green
>
> A
>
> 10
>
> April
>
> 3
>
> 4
>
> black
>
> A
>
> 17
>
> August
>
> 3
>
> 5
>
> red
>
> C
>
> 5
>
> December
>
> 5
>
> 6
>
> orange
>
> D
>
> 0
>
> January
>
> 13
>
> 7
>
> orange
>
> E
>
> 12
>
> January
>
> 5
>
> 8
>
> orange
>
> F
>
> 11
>
> February
>
> 8
>
> 9
>
> orange
>
> F
>
> 99
>
> July
>
> 23
>
> 10
>
> orange
>
> F
>
> 70
>
> May
>
> 7
>
> 11
>
> black
>
> A
>
> 77
>
> June
>
> 11
>
> 12
>
> green
>
> B
>
> 87
>
> April
>
> 33
>
> 13
>
> black
>
> A
>
> 79
>
> August
>
> 9
>
> 14
>
> green
>
> A
>
> 68
>
> December
>
> 14
>
> 15
>
> black
>
> C
>
> 90
>
> January
>
> 31
>
> 16
>
> green
>
> D
>
> 79
>
> January
>
> 11
>
> 17
>
> black
>
> E
>
> 101
>
> February
>
> 17
>
> 18
>
> red
>
> F
>
> 90
>
> July
>
> 21
>
> 19
>
> red
>
> F
>
> 112
>
> February
>
> 13
>
> 20
>
> red
>
> F
>
> 101
>
> July
>
> 20
>
>
>

[[alternative HTML version deleted]]

__
R-help@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.

Re: [R] nested for loop with data table

2017-05-02 Thread Jim Lemon
Hi Ek,
I think you want your example to look like this:

Sample<-read.table(text=
"Num Color Grade Value Month Day
1 yellow A 20 May 1
2 green B 25 June 2
3 green A 10 April 3
4 black A 17 August 3
5 red C 5 December 5
6 orange D 0 January 13
7 orange E 12 January 5
8 orange F 11 February 8
9 orange F 99 July 23
10 orange F 70 May 7
11 black A 77 June 11
12 green B 87 April 33
13 black A 79 August 9
14 green A 68 December 14
15 black C 90 January 31
16 green D 79 January 11
17 black E 101 February 17
18 red F 90 July 21
19 red F 112 February 13
20 red F 101 July 20",
header=TRUE)
AAA<-Sample[Sample$Num < 5 & Sample$Day < 3,]
BBB<-Sample[Sample$Num > 15 & Sample$Day > 13,]
for(i in 1:length(AAA)) {
 for(j in 1:length(BBB)) {
  ...
 }
}

except in data.table notation. However, I can't work out what you want
to do in the loop.

Jim


On Wed, May 3, 2017 at 2:35 AM, Ek Esawi  wrote:
> I have a huge data file; a sample is listed below. I am using the package
> data table to process the file and I am stuck on one issue and need some
> feedback. I used fread to create a data table. Then I divided the data
> table (named File1) into 10 general subsets using common table commands
> such as:
>
>
>
> AAA <- File1[Num<5&day>15]
>
> BBB <- File1[Num>15&day<10]
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
> …..
>
>
>
> I wanted to divide and count each of the above subsets based on a set of
> parameters common to all subsets. I did the following to go through each
> subset and it works:
>
> For (I in 1: length (AAA)) {
>
>   aa <- c(AAA[color==”green”&grade==”a”,month==”Januray” .N],[
> AAA[color==”green”&grade==”b”& month==”June”’ .N])
>
> }
>
>
>
> The question: I don’t want to have a separate loop for each subset (10
> loops). Instead, I was hoping to have 2 nested loops in the form below:
>
>
>
> For (I in 1:N)){
>
>   For (j in 1:M){
>
>
>
> }
>
> }
>
>
>
>  Sample
>
>
> Num
>
> Color
>
> Grade
>
> Value
>
> Month
>
> Day
>
> 1
>
> yellow
>
> A
>
> 20
>
> May
>
> 1
>
> 2
>
> green
>
> B
>
> 25
>
> June
>
> 2
>
> 3
>
> green
>
> A
>
> 10
>
> April
>
> 3
>
> 4
>
> black
>
> A
>
> 17
>
> August
>
> 3
>
> 5
>
> red
>
> C
>
> 5
>
> December
>
> 5
>
> 6
>
> orange
>
> D
>
> 0
>
> January
>
> 13
>
> 7
>
> orange
>
> E
>
> 12
>
> January
>
> 5
>
> 8
>
> orange
>
> F
>
> 11
>
> February
>
> 8
>
> 9
>
> orange
>
> F
>
> 99
>
> July
>
> 23
>
> 10
>
> orange
>
> F
>
> 70
>
> May
>
> 7
>
> 11
>
> black
>
> A
>
> 77
>
> June
>
> 11
>
> 12
>
> green
>
> B
>
> 87
>
> April
>
> 33
>
> 13
>
> black
>
> A
>
> 79
>
> August
>
> 9
>
> 14
>
> green
>
> A
>
> 68
>
> December
>
> 14
>
> 15
>
> black
>
> C
>
> 90
>
> January
>
> 31
>
> 16
>
> green
>
> D
>
> 79
>
> January
>
> 11
>
> 17
>
> black
>
> E
>
> 101
>
> February
>
> 17
>
> 18
>
> red
>
> F
>
> 90
>
> July
>
> 21
>
> 19
>
> red
>
> F
>
> 112
>
> February
>
> 13
>
> 20
>
> red
>
> F
>
> 101
>
> July
>
> 20
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.

Re: [R] nested for loop with data table

2017-05-02 Thread Boris Steipe
There's a lot that doesn't make sense here. I think what you need to do is 
produce a small, reproducible example, post that with dput() and state your 
question more clearly - including what you have tried and what didn't work. 
You'll probably be amazed how quickly you will get good advice if 
_you_only_follow_the_posting_guide_.

B.




> On May 2, 2017, at 12:35 PM, Ek Esawi  wrote:
> 
> I have a huge data file; a sample is listed below. I am using the package
> data table to process the file and I am stuck on one issue and need some
> feedback. I used fread to create a data table. Then I divided the data
> table (named File1) into 10 general subsets using common table commands
> such as:
> 
> 
> 
> AAA <- File1[Num<5&day>15]
> 
> BBB <- File1[Num>15&day<10]
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> 
> 
> I wanted to divide and count each of the above subsets based on a set of
> parameters common to all subsets. I did the following to go through each
> subset and it works:
> 
> For (I in 1: length (AAA)) {
> 
>  aa <- c(AAA[color==”green”&grade==”a”,month==”Januray” .N],[
> AAA[color==”green”&grade==”b”& month==”June”’ .N])
> 
> }
> 
> 
> 
> The question: I don’t want to have a separate loop for each subset (10
> loops). Instead, I was hoping to have 2 nested loops in the form below:
> 
> 
> 
> For (I in 1:N)){
> 
>  For (j in 1:M){
> 
> 
> 
> }
> 
> }
> 
> 
> 
> Sample
> 
> 
> Num
> 
> Color
> 
> Grade
> 
> Value
> 
> Month
> 
> Day
> 
> 1
> 
> yellow
> 
> A
> 
> 20
> 
> May
> 
> 1
> 
> 2
> 
> green
> 
> B
> 
> 25
> 
> June
> 
> 2
> 
> 3
> 
> green
> 
> A
> 
> 10
> 
> April
> 
> 3
> 
> 4
> 
> black
> 
> A
> 
> 17
> 
> August
> 
> 3
> 
> 5
> 
> red
> 
> C
> 
> 5
> 
> December
> 
> 5
> 
> 6
> 
> orange
> 
> D
> 
> 0
> 
> January
> 
> 13
> 
> 7
> 
> orange
> 
> E
> 
> 12
> 
> January
> 
> 5
> 
> 8
> 
> orange
> 
> F
> 
> 11
> 
> February
> 
> 8
> 
> 9
> 
> orange
> 
> F
> 
> 99
> 
> July
> 
> 23
> 
> 10
> 
> orange
> 
> F
> 
> 70
> 
> May
> 
> 7
> 
> 11
> 
> black
> 
> A
> 
> 77
> 
> June
> 
> 11
> 
> 12
> 
> green
> 
> B
> 
> 87
> 
> April
> 
> 33
> 
> 13
> 
> black
> 
> A
> 
> 79
> 
> August
> 
> 9
> 
> 14
> 
> green
> 
> A
> 
> 68
> 
> December
> 
> 14
> 
> 15
> 
> black
> 
> C
> 
> 90
> 
> January
> 
> 31
> 
> 16
> 
> green
> 
> D
> 
> 79
> 
> January
> 
> 11
> 
> 17
> 
> black
> 
> E
> 
> 101
> 
> February
> 
> 17
> 
> 18
> 
> red
> 
> F
> 
> 90
> 
> July
> 
> 21
> 
> 19
> 
> red
> 
> F
> 
> 112
> 
> February
> 
> 13
> 
> 20
> 
> red
> 
> F
> 
> 101
> 
> July
> 
> 20
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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.

__
R-help@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.

[R] nested for loop with data table

2017-05-02 Thread Ek Esawi
I have a huge data file; a sample is listed below. I am using the package
data table to process the file and I am stuck on one issue and need some
feedback. I used fread to create a data table. Then I divided the data
table (named File1) into 10 general subsets using common table commands
such as:



AAA <- File1[Num<5&day>15]

BBB <- File1[Num>15&day<10]

…..

…..

…..

…..

…..

…..



I wanted to divide and count each of the above subsets based on a set of
parameters common to all subsets. I did the following to go through each
subset and it works:

For (I in 1: length (AAA)) {

  aa <- c(AAA[color==”green”&grade==”a”,month==”Januray” .N],[
AAA[color==”green”&grade==”b”& month==”June”’ .N])

}



The question: I don’t want to have a separate loop for each subset (10
loops). Instead, I was hoping to have 2 nested loops in the form below:



For (I in 1:N)){

  For (j in 1:M){



}

}



 Sample


Num

Color

Grade

Value

Month

Day

1

yellow

A

20

May

1

2

green

B

25

June

2

3

green

A

10

April

3

4

black

A

17

August

3

5

red

C

5

December

5

6

orange

D

0

January

13

7

orange

E

12

January

5

8

orange

F

11

February

8

9

orange

F

99

July

23

10

orange

F

70

May

7

11

black

A

77

June

11

12

green

B

87

April

33

13

black

A

79

August

9

14

green

A

68

December

14

15

black

C

90

January

31

16

green

D

79

January

11

17

black

E

101

February

17

18

red

F

90

July

21

19

red

F

112

February

13

20

red

F

101

July

20

[[alternative HTML version deleted]]

__
R-help@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.

Re: [R] Nested for loop help please.

2014-01-12 Thread arun


Hi Mathew,

You must have noticed that I used:
output <-vector() # instead of output=array(NA,c(length(pick.a), 
length(pick.d)))

The below methods should give the results:
Method1:
res <- t(sapply(pick.a,function(x) sapply(pick.d,function(y) {ts1 <- 
sum(t.sham(m.sham,x,y)); tc1 <- sum(t.control(m.control,x)); ts1/tc1})))
dim(res)
#[1] 200 100

Method2:### Creating a matrix of NAs
output <- array(NA,c(length(pick.a),length(pick.d)))
 for(count in 1:length(pick.a)){
 for(count1 in 1:length(pick.d)){
 ts1= sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
 tc1=sum(t.control(m.control,pick.a[count]))
 output[count,count1] <- ts1/tc1
 }
 }
dim(output)
#[1] 200 100

identical(res,output)
#[1] TRUE


###Method 3:
output1 <-vector() #instead of defining it as matrix
 for(count in 1:length(pick.a)){
  for(count1 in 1:length(pick.d)){
  ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
 tc1 <- sum(t.control(m.control,pick.a[count]))
 output1= c(output1,ts1/tc1)
  }
  }
 m1 <- matrix(output1,length(pick.a),length(pick.d),byrow=TRUE)
 identical(m1,res)
#[1] TRUE
 
A.K.




On Sunday, January 12, 2014 12:49 PM, Mathew Nagendran 
 wrote:
Hi Arun, thank you for all your help. Considering I only started using R a few 
months ago, your help is very valuable to me.

I have taken the steps required to make my nested for loop work. The only 
problem I face now is trying to get my matrix to fill with outputs rather than 
NA. Is there something I can do to resolve this?

m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) 
m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) 

t.control=function(m, a){(1-exp(-a*m))} 
t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}

t.ratio=function(ts, tc){(ts/tc)} 

pick.a=seq(0.01,2,by=0.01) #set of a values defined
pick.d=seq(0.01,1,by=0.01) #set of d values defined

output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
columns.

for(count in 1:length(pick.a)){
    for(count1 in 1:length(pick.d)){
        ts1=sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
        tc1=sum(t.control(m.control,pick.a[count]))
        output=c(output,ts1/tc1)
    }
}
m1=matrix(output,200,100)
print(m1)



From: arun 
Sent: Saturday, January 11, 2014 6:00 PM
To: Mathew Nagendran
Subject: Re: [R] Nested for loop help please.

Hi,
I guess you need to replace t() with t.control() or t.sham()
output <-vector()
for(count in 1:length(pick.a)){
for(count1 in 1:length(pick.d)){
ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1 <- sum(t.control(m.control,pick.a[count]))
output= c(output,ts1/tc1)
}
}
m1 <- matrix(output,200,100)
dim(m1)
#[1] 200 100
A.K.




On Saturday, January 11, 2014 3:20 PM, Mathew Nagendran 
 wrote:
Hi all I am relatively new to R. I am trying to create a nested for loop but i 
keep getting an error message (unused argument). Can someone help me find out 
where I am goign wrong?

> m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
> m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)
>
> t.control=function(m, a){(1-exp(-a*m))}
> t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}
>
> t.ratio=function(ts, tc){(ts/tc)}
>
> pick.a=seq(0.01,2,by=0.01) #set of a values defined
> pick.d=seq(0.01,1,by=0.01) #set of d values defined
>
> output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
> ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
> columns.
>
> for(count in 1:length(pick.a)){
+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
  unused argument(s) (pick.a[count], pick.d[count])

    [[alternative HTML version deleted]]

__
R-help@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.

__
R-help@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.


Re: [R] Nested for loop help please.

2014-01-11 Thread arun




Hi,
I guess you need to replace t() with t.control() or t.sham()
output <-vector()
for(count in 1:length(pick.a)){
 for(count1 in 1:length(pick.d)){
 ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1 <- sum(t.control(m.control,pick.a[count]))
output= c(output,ts1/tc1)
 }
 }
m1 <- matrix(output,200,100)
 dim(m1)
#[1] 200 100
A.K.





On Saturday, January 11, 2014 3:20 PM, Mathew Nagendran 
 wrote:
Hi all I am relatively new to R. I am trying to create a nested for loop but i 
keep getting an error message (unused argument). Can someone help me find out 
where I am goign wrong?

> m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
> m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)
>
> t.control=function(m, a){(1-exp(-a*m))}
> t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}
>
> t.ratio=function(ts, tc){(ts/tc)}
>
> pick.a=seq(0.01,2,by=0.01) #set of a values defined
> pick.d=seq(0.01,1,by=0.01) #set of d values defined
>
> output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
> ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
> columns.
>
> for(count in 1:length(pick.a)){
+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
  unused argument(s) (pick.a[count], pick.d[count])

    [[alternative HTML version deleted]]

__
R-help@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.


__
R-help@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.


Re: [R] Nested for loop help please.

2014-01-11 Thread William Dunlap
> > for(count in 1:length(pick.a)){
> + for(count in 1:length(pick.d)){
> + ts=sum(t(m.sham,pick.a[count],pick.d[count]))

The variable 'count' in the inner loop is overwriting the value
of 'count' set in the outer loop.  Use different names in
the different loops.
   for(count.a in seq_along(pick.a)){
   for(count.d in seq_along(pick.d)){
   FUN(pick.a[count.a], pick.d[count.d])

(I wrote 'FUN' because the t() function is misused in the example.)

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf
> Of Mathew Nagendran
> Sent: Saturday, January 11, 2014 10:46 AM
> To: r-help@r-project.org
> Subject: [R] Nested for loop help please.
> 
> Hi all I am relatively new to R. I am trying to create a nested for loop but 
> i keep getting
> an error message (unused argument). Can someone help me find out where I am 
> goign
> wrong?
> 
> > m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
> > m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)
> >
> > t.control=function(m, a){(1-exp(-a*m))}
> > t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}
> >
> > t.ratio=function(ts, tc){(ts/tc)}
> >
> > pick.a=seq(0.01,2,by=0.01) #set of a values defined
> > pick.d=seq(0.01,1,by=0.01) #set of d values defined
> >
> > output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
> > ratios. a
> values (0.01-2) in column 1 and d values (0.01-1) in the other columns.
> >
> > for(count in 1:length(pick.a)){
> + for(count in 1:length(pick.d)){
> + ts=sum(t(m.sham,pick.a[count],pick.d[count]))
> + tc=sum(t(m.control,pick.a[count]))
> + output[count,2]= (ts/tc)
> + }
> + print(output)
> + }
> Error in t(m.sham, pick.a[count], pick.d[count]) :
>   unused argument(s) (pick.a[count], pick.d[count])
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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.

__
R-help@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.


Re: [R] Nested for loop help please.

2014-01-11 Thread Rui Barradas

Hello,

You are using the function ?t(), matrix transpose, with more than one 
argument. What is that supposed to do? The error message says that the 
other two arguments are not used (because they are illegal). And you 
don't need to transpose a vector to sum its components.


Hope this helps,

Rui Barradas

Em 11-01-2014 18:46, Mathew Nagendran escreveu:

Hi all I am relatively new to R. I am trying to create a nested for loop but i 
keep getting an error message (unused argument). Can someone help me find out 
where I am goign wrong?


m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)

t.control=function(m, a){(1-exp(-a*m))}
t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}

t.ratio=function(ts, tc){(ts/tc)}

pick.a=seq(0.01,2,by=0.01) #set of a values defined
pick.d=seq(0.01,1,by=0.01) #set of d values defined

output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
columns.

for(count in 1:length(pick.a)){

+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
   unused argument(s) (pick.a[count], pick.d[count])

[[alternative HTML version deleted]]

__
R-help@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.



__
R-help@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.


[R] Nested for loop help please.

2014-01-11 Thread Mathew Nagendran
Hi all I am relatively new to R. I am trying to create a nested for loop but i 
keep getting an error message (unused argument). Can someone help me find out 
where I am goign wrong?

> m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
> m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)
>
> t.control=function(m, a){(1-exp(-a*m))}
> t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}
>
> t.ratio=function(ts, tc){(ts/tc)}
>
> pick.a=seq(0.01,2,by=0.01) #set of a values defined
> pick.d=seq(0.01,1,by=0.01) #set of d values defined
>
> output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc 
> ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other 
> columns.
>
> for(count in 1:length(pick.a)){
+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
  unused argument(s) (pick.a[count], pick.d[count])

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Nested For Loop

2012-06-29 Thread R. Michael Weylandt
Don't (can't) use paste0 with sep='' -- that's redundant, and I'm surprised not 
an error. 

Michael

On Jun 29, 2012, at 7:18 PM, arun  wrote:

> Hi,
> 
> I knew that you already got many replies.
> 
> Here is my contribution.
> dat1<-paste0(expand.grid(d,e)$Var1,expand.grid(d,e)$Var2,sep="")
> 
> or
> dat2<-as.vector(rbind(paste0(d,e[1],sep=""),paste0(d,e[2],sep=""),paste0(d,e[3],sep="")))
> 
> 
> #Still Bert's one line is the shortest
> 
> 
> A.K.
> 
> 
> 
> - Original Message -
> From: arun.gurubaramurugeshan 
> To: r-help@r-project.org
> Cc: 
> Sent: Thursday, June 28, 2012 5:03 PM
> Subject: [R] Nested For Loop
> 
> I am creating a nested for loop and following are the codes I'm using, but I
> am not acheiving what I want.
> 
> I have a vector 
> 
> d<-151:159
> 
> I have another vector
> 
> e<-e<-c("apple", "orange", "banana")
> 
> I need to create f as
> 151apple
> 151orange
> 151banana
> .
> .
> 159apple
> 159orange
> 159banana
> 
> Here is how I wrote nested for loop...
> 
> for (i in 1:length(d))
> { for (j in 1:length(e))
> {
> x[j]<-paste(d[i],e[j],sep="")
> print(x[j])
> }
> }
> 
> The result of the above codes is
> 
>> for (i in 1:length(d))
> + { for (j in 1:length(e))
> + {
> + x[j]<-paste(d[i],e[j],sep="")
> + print(x[j])
> + }
> + }
> [1] "151apple"
> [1] "151orange"
> [1] "151banana"
>> 
> 
> 
> What do I need to do this looping produce the desired result.
> 
> Thanks
> Arun
> 
> 
> 
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> __
> R-help@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.
> 
> __
> R-help@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.

__
R-help@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.


Re: [R] Nested For Loop

2012-06-29 Thread arun
Hi,

I knew that you already got many replies.

Here is my contribution.
dat1<-paste0(expand.grid(d,e)$Var1,expand.grid(d,e)$Var2,sep="")

or
dat2<-as.vector(rbind(paste0(d,e[1],sep=""),paste0(d,e[2],sep=""),paste0(d,e[3],sep="")))


#Still Bert's one line is the shortest


A.K.



- Original Message -
From: arun.gurubaramurugeshan 
To: r-help@r-project.org
Cc: 
Sent: Thursday, June 28, 2012 5:03 PM
Subject: [R] Nested For Loop

I am creating a nested for loop and following are the codes I'm using, but I
am not acheiving what I want.

I have a vector 

d<-151:159

I have another vector

e<-e<-c("apple", "orange", "banana")

I need to create f as
151apple
151orange
151banana
.
.
159apple
159orange
159banana

Here is how I wrote nested for loop...

for (i in 1:length(d))
{ for (j in 1:length(e))
{
x[j]<-paste(d[i],e[j],sep="")
print(x[j])
}
}

The result of the above codes is

> for (i in 1:length(d))
+ { for (j in 1:length(e))
+ {
+ x[j]<-paste(d[i],e[j],sep="")
+ print(x[j])
+ }
+ }
[1] "151apple"
[1] "151orange"
[1] "151banana"
> 


What do I need to do this looping produce the desired result.

Thanks
Arun






--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.

__
R-help@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.


Re: [R] Nested For Loop

2012-06-29 Thread David Winsemius


On Jun 28, 2012, at 5:03 PM, arun.gurubaramurugeshan wrote:

I am creating a nested for loop and following are the codes I'm  
using, but I

am not acheiving what I want.

I have a vector

d<-151:159

I have another vector

e<-e<-c("apple", "orange", "banana")




I need to create f as
151apple
151orange
151banana
.
.
159apple
159orange
159banana



I admit that I think Bert's solution is way kewler than mine, but I  
would not have thought of using interaction() to mimic the "crossed"  
use of rep()


paste(as.character(rep(d, each=length(e))), rep(e, times=length(d) ),  
sep="")


(Looking at the code for interaction one sees that this is how it was  
coded.)


The other R functions to remember for this sort of loop-avoidance are  
'outer' and 'expand.grid'.


> outer(d,e,paste, sep="")
 [,1]   [,2][,3]
[1,] "151apple" "151orange" "151banana"
[2,] "152apple" "152orange" "152banana"
[3,] "153apple" "153orange" "153banana"
[4,] "154apple" "154orange" "154banana"
[5,] "155apple" "155orange" "155banana"

as.vector( outer(d, e, paste, sep="") )

--
David.



Here is how I wrote nested for loop...

for (i in 1:length(d))
{ for (j in 1:length(e))
{
x[j]<-paste(d[i],e[j],sep="")
print(x[j])
}
}

The result of the above codes is


for (i in 1:length(d))

+ { for (j in 1:length(e))
+ {
+ x[j]<-paste(d[i],e[j],sep="")
+ print(x[j])
+ }
+ }
[1] "151apple"
[1] "151orange"
[1] "151banana"





What do I need to do this looping produce the desired result.

Thanks
Arun






--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


David Winsemius, MD
West Hartford, CT

__
R-help@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.


Re: [R] Nested For Loop

2012-06-29 Thread Bert Gunter
reversed them, Should be:

levels(interaction(d,e,sep="")
>
> of course.

-- Bert


On Fri, Jun 29, 2012 at 2:46 AM, Bert Gunter  wrote:

> ... or even simpler:
>
> levels(interaction(e,d,sep=""))
>
> ... and PLEASE read "An Introduction to R" before further posting to learn
> how to program in R rather than trying to fit what you do in other
> languages to R.
>
> Cheers,
> Bert
>
> On Fri, Jun 29, 2012 at 12:48 AM, Rui Barradas wrote:
>
>> Hello,
>>
>> Try, in one line and no loops, surely not nested,
>>
>> apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste,
>> collapse="")
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu:
>>
>>> I am creating a nested for loop and following are the codes I'm using,
>>> but I
>>> am not acheiving what I want.
>>>
>>> I have a vector
>>>
>>> d<-151:159
>>>
>>> I have another vector
>>>
>>> e<-e<-c("apple", "orange", "banana")
>>>
>>> I need to create f as
>>> 151apple
>>> 151orange
>>> 151banana
>>> .
>>> .
>>> 159apple
>>> 159orange
>>> 159banana
>>>
>>> Here is how I wrote nested for loop...
>>>
>>> for (i in 1:length(d))
>>> { for (j in 1:length(e))
>>> {
>>> x[j]<-paste(d[i],e[j],sep="")
>>> print(x[j])
>>> }
>>> }
>>>
>>> The result of the above codes is
>>>
>>>  for (i in 1:length(d))

>>> + { for (j in 1:length(e))
>>> + {
>>> + x[j]<-paste(d[i],e[j],sep="")
>>> + print(x[j])
>>> + }
>>> + }
>>> [1] "151apple"
>>> [1] "151orange"
>>> [1] "151banana"
>>>


>>>
>>> What do I need to do this looping produce the desired result.
>>>
>>> Thanks
>>> Arun
>>> 
>>>
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context: http://r.789695.n4.nabble.com/**
>>> Nested-For-Loop-tp4634804.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> __**
>>> R-help@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.
>>>
>>>
>> __**
>> R-help@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.
>>
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
>
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
>
>


-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Nested For Loop

2012-06-29 Thread Bert Gunter
... or even simpler:

levels(interaction(e,d,sep=""))

... and PLEASE read "An Introduction to R" before further posting to learn
how to program in R rather than trying to fit what you do in other
languages to R.

Cheers,
Bert

On Fri, Jun 29, 2012 at 12:48 AM, Rui Barradas  wrote:

> Hello,
>
> Try, in one line and no loops, surely not nested,
>
> apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste,
> collapse="")
>
> Hope this helps,
>
> Rui Barradas
>
> Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu:
>
>> I am creating a nested for loop and following are the codes I'm using,
>> but I
>> am not acheiving what I want.
>>
>> I have a vector
>>
>> d<-151:159
>>
>> I have another vector
>>
>> e<-e<-c("apple", "orange", "banana")
>>
>> I need to create f as
>> 151apple
>> 151orange
>> 151banana
>> .
>> .
>> 159apple
>> 159orange
>> 159banana
>>
>> Here is how I wrote nested for loop...
>>
>> for (i in 1:length(d))
>> { for (j in 1:length(e))
>> {
>> x[j]<-paste(d[i],e[j],sep="")
>> print(x[j])
>> }
>> }
>>
>> The result of the above codes is
>>
>>  for (i in 1:length(d))
>>>
>> + { for (j in 1:length(e))
>> + {
>> + x[j]<-paste(d[i],e[j],sep="")
>> + print(x[j])
>> + }
>> + }
>> [1] "151apple"
>> [1] "151orange"
>> [1] "151banana"
>>
>>>
>>>
>>
>> What do I need to do this looping produce the desired result.
>>
>> Thanks
>> Arun
>> 
>>
>>
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/**
>> Nested-For-Loop-tp4634804.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __**
>> R-help@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.
>>
>>
> __**
> R-help@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.
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Nested For Loop

2012-06-29 Thread Rui Barradas

Hello,

Try, in one line and no loops, surely not nested,

apply(rev(expand.grid(e, d, stringsAsFactors = FALSE)), 1, paste, 
collapse="")


Hope this helps,

Rui Barradas

Em 28-06-2012 22:03, arun.gurubaramurugeshan escreveu:

I am creating a nested for loop and following are the codes I'm using, but I
am not acheiving what I want.

I have a vector

d<-151:159

I have another vector

e<-e<-c("apple", "orange", "banana")

I need to create f as
151apple
151orange
151banana
.
.
159apple
159orange
159banana

Here is how I wrote nested for loop...

for (i in 1:length(d))
{ for (j in 1:length(e))
{
x[j]<-paste(d[i],e[j],sep="")
print(x[j])
}
}

The result of the above codes is


for (i in 1:length(d))

+ { for (j in 1:length(e))
+ {
+ x[j]<-paste(d[i],e[j],sep="")
+ print(x[j])
+ }
+ }
[1] "151apple"
[1] "151orange"
[1] "151banana"





What do I need to do this looping produce the desired result.

Thanks
Arun






--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.



__
R-help@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.


[R] Nested For Loop

2012-06-28 Thread arun.gurubaramurugeshan
I am creating a nested for loop and following are the codes I'm using, but I
am not acheiving what I want.

I have a vector 

d<-151:159

I have another vector

e<-e<-c("apple", "orange", "banana")

I need to create f as
151apple
151orange
151banana
.
.
159apple
159orange
159banana

Here is how I wrote nested for loop...

for (i in 1:length(d))
{ for (j in 1:length(e))
{
x[j]<-paste(d[i],e[j],sep="")
print(x[j])
}
}

The result of the above codes is

> for (i in 1:length(d))
+ { for (j in 1:length(e))
+ {
+ x[j]<-paste(d[i],e[j],sep="")
+ print(x[j])
+ }
+ }
[1] "151apple"
[1] "151orange"
[1] "151banana"
> 


What do I need to do this looping produce the desired result.

Thanks
Arun






--
View this message in context: 
http://r.789695.n4.nabble.com/Nested-For-Loop-tp4634804.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.