[R] Passing data to t.test in loop

PIKAL Petr petr.pikal at precheza.cz
Mon Dec 8 15:39:31 CET 2014


Hi

Is this what you want?

by(data$time, list(data$size), function(x) t.test(x))

Petr

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Paul
> Johnston
> Sent: Tuesday, December 02, 2014 10:17 AM
> To: R-help at lists.R-project.org
> Subject: [R] Passing data to t.test in loop
>
> Dear All
>
> First post so sorry for any breaches of etiquette.
> I have a csv containing the results for a series of experiments which
> record the time taken for various sizes of iterations.
>
> "run_id","size","time"
> 1,100,1.00
> 2,200,2.100
> 3,100,1.100
> 4,200,2.100
> 5,200,1.900
> 6,300,4.00
> 7,200,2.5
> ...
>
> I read the data set, extract the results for each "size" and return
> various statistics.
> The only problem is I would like to iterate over the distinct sizes to
> do a t.test
> My code has a section commented #manual t.test but I have no luck with
> the attempt labelled #attempt to automate t.test
> I'm assuming it's my attempt to pass the data as an argument to
> t.test()
>
> Any pointers gratefully accepted but as I'm a learner hints rather than
> a solution are preferred.
>
> Cheers Paul
>
> getwd()
> setwd("c:/work/R/experiment1")
> # read raw experimental data from results file
> data <- read.csv("data1.csv", header = TRUE)
> data
> #create a new dataframe which has space for a record for each unique
> size of experiment
> # this is to collect collated statistics for each experiment
>
> var_list <- c("num_obs", "size_run",
> "sample_mean","sample_var","std_dev","se")
> var_list_length <- length(var_list)
> num_experiments <- length(unique(data$size))
> # create the dataframe
> df = data.frame(matrix(vector(), num_experiments , var_list_length,
> dimnames=list(c(),var_list)), stringsAsFactors=F)
> # it should be empty
> df
> # insert the experiment size
> df$size_run <- unique(data$size)
> # now it should have a single column filled
> # using
> df$size_run
> df
> # create a vector with the experiment sizes
>
> for (i in df$size_run)
> {
> # calculate the sample_variance of observations on a particular size
> df$sample_var[df$size == i] <- var(subset(data$time, data$size == i))
>
> # calculate the mean of the returned values for all experiments of the
> same size
> df$sample_mean[df$size == i] <- mean(subset(data$time, data$size ==i))
>
> # calculate the number of observations on a particular size
> df$num_obs[df$size == i] <- length(subset(data$time, data$size == i))
>
> # calculate the sd of the data
> df$std_dev[df$size == i] <- sd(subset(data$time, data$size == i))
>
> # calculate the standard error
> df$se[df$size == i] <- sd(subset(data$time, data$size
> ==i))/sqrt(length(subset(data$time, data$size ==i)))
> }
>
> df
>
> #manual t.test
> print("t.test for size  = 100")
> t.test(subset(data$time, data$size == 100))
>
> print("t.test for size  = 200")
> t.test(subset(data$time, data$size == 200))
>
> print("t.test for size  = 300")
> t.test(subset(data$time, data$size == 300))
>
> #attempt to automate t.test
> for (i in df$size_run)
> {
> print(i)
> a <- subset(data$time, data$size == i)
> print(a)
> t.test(a)
> }
>
>       [[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.

________________________________
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.


More information about the R-help mailing list