-1
replicate(10, rnorm(20))-> ExampleData

x<-2
ExampleData[x,7:9]->Example1
ExampleData[x,3:5]->Example2

t.test(Example1, Example2)

I would like to know how to feed a list of variables into "x". Like:

c(2, 3, 4, 5, 6)

and then get the results from the t.test back.

dww
  • 30,425
  • 5
  • 68
  • 111
T fish
  • 25
  • 6
  • that gives me an average of the five in the t.test. I want 5 separate t.tests. – T fish Jun 21 '18 at 20:39
  • See e.g. [here](https://stackoverflow.com/questions/9661469/r-t-test-over-all-columns) and [here](https://stackoverflow.com/questions/46741886/how-to-run-multiple-t-test-in-a-data-frame) – dww Jun 21 '18 at 20:43

1 Answers1

0

Write a loop

ExampleData <- data.frame(replicate(10,sample(0:1,1000,rep=TRUE)))
temp=c(2,3,4,5,6)
for(x in temp){

  ExampleData[x,7:9]->Example1
  ExampleData[x,3:5]->Example2

  print(t.test(Example1, Example2))

}

this will produce a t-test for each value in the temp-vector

Kyle
  • 695
  • 6
  • 24
  • Unfortunately this only provides me one t-test that is an average. Unless I am missing a command to make it print all 3 t-tests. Thanks! – T fish Jun 21 '18 at 21:12
  • 3 t tests?, you have 5 numbers in that vector..So you should be expecting 5 t-tests. You will need to add the results of each t-test to a vector. You are most likely only seeing the result of the last t-test. – Kyle Jun 21 '18 at 21:15
  • how do I print the other 4? – T fish Jun 21 '18 at 21:17
  • print(t.test(Example1, Example2)) I've just tested this with replicated data and it prints 5 t-tests. – Kyle Jun 21 '18 at 21:21
  • `> print(t.test(Example1, Example2))` `Welch Two Sample t-test` `data: Example1 and Example2` `t = -0.7379, df = 3.3481, p-value = 0.5089` `alternative hypothesis: true difference in means is not equal to 0` `95 percent confidence interval:` ` -3.073777 1.861241` `sample estimates:` `mean of x mean of y ` `-0.3863355 0.2199328` This is the only result I recieve – T fish Jun 21 '18 at 21:25
  • I’m not sure what to tell you if you look at the logic of the loop there is nothing wrong you loop five times printing a T test each time with a different number supplemented for x. You have to brainstorm and play around a little bit but it’s a very simple approach. Copy and paste my answer into R and run it and then tell me if it only printed one result. – Kyle Jun 21 '18 at 21:29
  • You do know that you have to write that print command within the loop right? – Kyle Jun 21 '18 at 21:36
  • Yeah... it worked after doing that. Thank you for the help. The documentation on loops wasnt particularly helpful. – T fish Jun 21 '18 at 21:50