Hi

see in line

> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Kristi Glover
> Sent: Friday, March 28, 2014 7:04 PM
> To: R-help
> Subject: [R] starta.sampling with many (1000 times) and average them
>
> Hi R Users,
> I was trying to select a sample with columns (measured data) by
> stratum. I was able to select rows by stratum. But, I wanted to repeat
> this procedures 1000 and want to take an average  from the 1000 times.
> I think it  is different than bootstraping since I wanted to select
> (rondomly) row with many variable at a time and repeat 1000 times
> then,average them.
> #-----------------------------
> I put here an example
>
> raw=structure(list(watershed = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
>  1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"),
>    year = c(2001, 2001, 2002, 2002, 2002, 2002, 2002, 2001,
>   2001, 2001, 2002, 2002, 2002), sp1 = c(18.38, 29.1, 90.72,
>   16.12, 49.12, 20.81, 65.1, 1.87, 72.99, 93.45, 38.44, 67.13,
>  45.71), sp2 = c(46.46, 94, 86.87, 46.91, 21.41, 92.82, 87.75,
> 16.18, 18.16, 18.76, 19.26, 52.73, 49.09), sp3 = c(86.9,
> 62.82, 74.32, 75.49, 20.17, 58.84, 16.51, 44.14, 44.39, 32.36,
> 53.28, 67.42, 33.37)), .Names = c("watershed", "year", "sp1",
> "sp2", "sp3"), class = "data.frame", row.names = c(NA, -13L))
>
> raw$group<-paste(as.character(raw$watershed),as.character(raw$year),
> sep="|")
>
> # strata.sampling -----
> strata.sampling <- function(data, group,size, method = NULL) {
>  require(sampling)
>   if (is.null(method)) method <- "srswor"
>   if (!method %in% c("srswor", "srswr"))
>     stop('method must be "srswor" or "srswr"')
>   temp <- data[order(data[[group]]), ]
>   ifelse(length(size) > 1,
>          size <- size,
>          ifelse(size < 1,
>                 size <- round(table(temp[group]) * size),
>                 size <- rep(size, times=length(table(temp[group])))))
>   strat = strata(temp, stratanames = names(temp[group]),
>                  size = size, method = method)
>   getdata(temp, strat)
> }
> #---------------
> attach(raw)
> test1<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and
> year
> test2<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and
> year
> test3<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and
> year
> test4<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and
> year
> ...
> ...
> ...
> # here I wanted to repeat this procedure 1000 times

Use list object for test

test <- vector("list", 1000)

after that you can make a for cycle

for (i in 1:1000) {

test[[i]] <- strata.sampling(raw, 'group', 1)

}

>
> test1000<-strata.sampling(raw, 'group', 2)# select 1 row by watershed
> and year
>
> # and then average of this 1000 tables
>
> testA<-average(test1[,3:5],test2[,3:5],test3[,3:5],.....test1000[,3:5])

What is average? Mean? You want to average all values from those 3 columns from 
all tables to one number?

result <- 0
for (i in 1:1000) {
temp <- mean(as.matrix(test[[i]][,3:5]))
result <- temp + result
}
testA <- result/1000

Alternatively you can make one data frame from the list

test.d <- do.call(rbind, test)

and use aggregate, ave or other summary functions

> Inform<-subset(test1, select=c(1,2,6,9)
> FinalData<-data.frame(Inform, testA)
>
> Would you give me some suggestion on how I can make a loop to repeat
> this procedures and average them.

Please be more specific in your request. Final part is ambiguous.

Regards
Petr

>
> Thanks
>
>       [[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.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
______________________________________________
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.

Reply via email to