[R] Counting
Phil Spector
spector at stat.berkeley.edu
Tue Nov 16 23:50:31 CET 2010
First, I would rename your function "samples" to "mksample"
to avoid confusion with the R function "sample".
Next, I would modify the function so that you are returning
a list of samples, instead of a list containing a list of
samples:
mksamples = function(data,num)
lapply(1:num,function(i) sample(data,n,replace=TRUE))
Now, using your example:
> set.seed(123)
> n = 10
> data = rnorm(n=10,mean=5,sd=2)
> data[1] = 100
> obj = mksamples(data,1000)
We can count the number of the samples in obj which do
not contain the value 100 as
> sum(sapply(obj,function(x)!any(x == 100)))
[1] 350
Hope this helps!
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Tue, 16 Nov 2010, ufuk beyaztas wrote:
>
> thank you very much for your idea,
> if i write code as;
> my data name is data.
>
> samples<-function(data,num){
> resamples<-lapply(1:num,function(i) sample(data,n,replace=TRUE))
> list(resamples=resamples)}
>
>> n=10
> data<-rnorm(n=10,mean=5,sd=2)
> data[1]=100
> obj<-samples(data,1000)
>
> i generate 1000 sample, i did not use 'boot'. 100 is a outlier in the data
> set and same stuation, some of samples not contain , some of samples contain
> once and some of them contain many times. Now can you tell me how i count
> how many samples are there not contain any outlier in the 1000 samples?
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Counting-tp3045756p3045842.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>
More information about the R-help
mailing list