# Re: [R] t.test using RSPerl

From: Henrik Bengtsson <hb_at_maths.lth.se>
Date: Wed 15 Jun 2005 - 02:06:34 EST

This has nothing to do with RSPerl, instead it has to do what kind of object you obtain and how these are print():ed. Typing the name of an object, say, 'res', at R prompt and pressing ENTER;

> res

is equivalent as typing

> print(res)

This is for convenience to the user. Basically, this is why you can do

> 1+1
[1] 2

without having to do

> print(1+1)
[1] 2

When you "transfer" the object from R to Perl you will, as expect, only receive the value of 'res', not the output from 'print(res)'. Here is an example illustrating the behavior in R:

> res <- t.test(1:10,y=c(7:20))
> length(res)
[1] 9
> str(res)
List of 9
\$ statistic : Named num -5.43
..- attr(*, "names")= chr "t"
\$ parameter : Named num 22
..- attr(*, "names")= chr "df"
\$ p.value : num 1.86e-05
\$ conf.int : atomic [1:2] -11.05 -4.95    ..- attr(*, "conf.level")= num 0.95
\$ estimate : Named num [1:2] 5.5 13.5    ..- attr(*, "names")= chr [1:2] "mean of x" "mean of y"   \$ null.value : Named num 0

..- attr(*, "names")= chr "difference in means"
\$ alternative: chr "two.sided"
\$ method     : chr "Welch Two Sample t-test"
\$ data.name  : chr "1:10 and c(7:20)"
• attr(*, "class")= chr "htest" > print(res)

Welch Two Sample t-test

data: 1:10 and c(7:20)
t = -5.4349, df = 21.982, p-value = 1.855e-05 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:
-11.052802 -4.947198
sample estimates:
mean of x mean of y

5.5 13.5

In your case, to capture what print(res) is outputting, you may want to look at ?capture.out, that is

> output <- capture.output(res)
> output # ...that is, print(output)

[1] ""
[2] "\tWelch Two Sample t-test"
[3] ""
[4] "data:  1:10 and c(7:20) "
[5] "t = -5.4349, df = 21.982, p-value = 1.855e-05"
[6] "alternative hypothesis: true difference in means is not equal to 0 "
[7] "95 percent confidence interval:"
[8] " -11.052802  -4.947198 "
[9] "sample estimates:"
[10] "mean of x mean of y "
[11] "      5.5      13.5 "

[12] ""

and transfer 'output' to Perl.

Cheers

Henrik

Wagle, Mugdha wrote:
> Hi,
>
> I've just started using R and RSPerl. I have some code as follows:
>
> &R::initR("--no-save");
> &R::call("t.test", (\@array1, \@array2));
>
> where @array1 and @array2 are both 1-dimensional arrays in Perl having 54675 elements each. On execution the output is as follows:
>
> Calling R function name `t.test', # arguments: 3
> 1) Arg type 3
> Got a reference to a value 10
> Here now!2) Arg type 3
> Got a reference to a value 10
> Here now!Calling R
> t.test(c(0, 6.24280675278087, 6.35175793656943, 5.76925805661511,
> 7.0789316246711, 7.4636498661157, 8.13730810691084, 8.78203131644273,
> 9.64502765609435, 9.95631242346133, 5.83129579495516, 6.8798700754926,
> 7.31814159140937.......(REST OF THE ARRAY ELEMENTS).....
> 4.91632461462501, 3.38099467434464,
> 3.91800507710569, 3.23867845216438, 3.38439026334577, 4.64918707140487,
> 3.23474917402449, 3.62966009445396, 3.36729582998647, 3.91999117507732
> ))
> Performed the call, result has length 9
>
> My question is : with other functions such as sum and log10, the actual values of the result are displayed. Here the call seems to have worked but the output is not what you get when running t.test directly on the R command prompt..
>
> data: data4[2] and data4[3]
> t = 0.2186, df = 109.847, p-value = 0.8274
> alternative hypothesis: true difference in means is not equal to 0
> 95 percent confidence interval:
> -3722.830 4645.723
> sample estimates:
> mean of x mean of y
> 6185.139 5723.693
>
> which is what I had expected, after seeing the outputs in the case of simpler functions like sum. Could anyone please tell me how I can obtain the output I expect(i.e. the same as the command line output....giving values of t, p-value and the means)?
>
> Thank you very much for the help!!
>
> Sincerely,
> Mugdha Wagle
> Hartwell Center for Bioinformatics and Biotechnology,
> St.Jude Children's Research Hospital, Memphis TN 38105
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help