Dea'R' helpers
I have following data -
prob = c(0.1, 0.2, 0.3, 0.4, 0.5)
frequency = c(100, 75, 45, 30, 25)
no_trials = c(10, 8, 6, 4, 2)
freq1 = rbinom(frequency[1], no_trials[1], prob[1])
freq2 = rbinom(frequency[2], no_trials[2], prob[2])
freq3 = rbinom(frequency[3], no_trials[3], prob[3])
freq4 = rbinom(frequency[4], no_trials[4], prob[4])
freq5 = rbinom(frequency[5], no_trials[5], prob[5])
total_frequency = c(freq1, freq2, freq3, freq4, freq5)
total_frequency = as.numeric(lapply(total_frequency, function(x){replace(x, x
== 0, 1)}))
This helps me to generate (sum(frequency) = 275) non zero binomial numbers as
total_frequency
[1] 1 1 1 2 1 1 1 2 1 1 1 1 1 1 3 2 1 1 1 1 1 1 1 1 1 3 1 1 2 1 3 1 1 1 3 1 1
1 1 1 1 4 3 1 1 1 2 1 1 1 2 3 2 1 1 1 1 1 1
1
[61] 2 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 2 3 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1
1 1 1 3 2 3 1 3 1 2 3 1 1 1 1 2 1 3 1 2 1 1 2
[121] 3 1 2 1 3 3 3 1 5 2 3 2 1 3 2 1 2 1 1 1 3 1 4 1 1 3 3 1 1 3 1 2 3 2 1 2 5
1 2 2 1 1 1 1 2 2 2 3 1 1 3 1 1 1 1 3 1 3 1 2
[181] 1 2 1 2 1 2 1 2 2 4 2 2 1 4 1 1 3 2 4 1 1 3 1 3 1 1 1 2 2 1 3 1 1 4 1 1 3
2 1 3 3 1 1 2 1 1 2 3 3 1 1 2 3 2 1 1 1 2 1 1
[241] 1 3 2 1 1 2 3 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1
## My Problem
However I am not sure whether I will be dealing with only "5" numbers. So I can
have 'n' no.s of frequencies, no of trials and even probabilities. Thus I need
to generate these numbers using a loop. Assuming I am once again dealing with
above data only, I have tried to modify my R code as given below.
n = length(frequency)
freq = NULL
for (i in 1:n)
{
freq[i] = rbinom(frequency[i], no_trials[i],
prob[i])
}
I get following errors -
Warning messages:
1: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
number of items to replace is not a multiple of replacement length
2: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
number of items to replace is not a multiple of replacement length
3: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
number of items to replace is not a multiple of replacement length
4: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
number of items to replace is not a multiple of replacement length
5: In freq[i] = rbinom(frequency[i], no_trials[i], prob[i]) :
number of items to replace is not a multiple of replacement length
I understand I have written a stupid code. I even tried to define freq in a
matrix form as
freq <- matrix(data=0, nrow=n, ncol=1) but the result is
same.
Please guide me as how I should be writing the required loop in order to take
care of variable size of frequency input.
Thanking you in advance for the guidance.
Sarah
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.