0

The "do" command from the mosaic package nicely returns a data frame by default.

require(mosaic)
require(Sleuth3)


nulldist1 <- do(10)*t.test(Incidents~shuffle(Launch),
                           data=case0401,var.equal=TRUE)$statistic
class(nulldist1)

Now, I'd like to use the replicate command to do the same thing. Is there an argument that one can set in the replicate command that will force it to return a data frame? I tried simplify="data.frame" but that didn't work.

nulldist2 <- replicate(10,{
  t.test(Incidents~shuffle(Launch),
         data=case0401,var.equal=TRUE)$statistic
})
class(nulldist2)

Any thoughts?

David
  • 981
  • 1
  • 15
  • 27

2 Answers2

1
data.frame(result = replicate(10,t.test(rnorm(100),rnorm(100))$statistic))
Rohit Das
  • 1,962
  • 3
  • 14
  • 23
  • I'll rephrase; this is redundant to assign to `result` in a data frame call. Even though it works, `as.data.frame()` or even `data.frame()` without assigning to `result` is clearer and more direct (with the latter calling `as.data.frame` as described in `?data.frame` - hence cut out the middle man and call it directly!) – Gavin Simpson May 29 '14 at 23:14
  • The command nulldist2 <- data.frame(t=replicate(10,{ t.test(Incidents~shuffle(Launch), data=case0401,var.equal=TRUE)$statistic })) is like the command df<-data.frame(x=c(1,2,3)), it doesn't create a variable t, it makes the name of the first column t, which is what I wanted so that the replicate command gives exactly the same result as the do command. – David May 30 '14 at 02:44
  • The whole point of the "result = " is to rename the column. Without it the column will have a long unwieldy name. Its completely optional, I just like it cleaner this way. – Rohit Das Jun 04 '14 at 17:01
0

If you read ?replicate you'll notice that there is no simplify = "data.frame" option and hence it is no surprise that that didn't work.

As replicate() returns an array, and assuming the result is a 2-d array, just use as.data.frame() on the object nulldist2

nulldist2 <- as.data.frame(nulldist2)

Here's an example:

> arr <- replicate(10, quantile(rnorm(1000)))
> arr
           [,1]        [,2]        [,3]         [,4]        [,5]        [,6]
0%   -3.3574313 -3.09705035 -2.85009578 -3.140181733 -2.94235439 -3.24334919
25%  -0.6610031 -0.68506227 -0.66891111 -0.681785814 -0.66985211 -0.60439317
50%   0.0576617  0.05899849 -0.01235571  0.007168725  0.07139977  0.07511633
75%   0.7584004  0.67879534  0.69335820  0.659121437  0.72854080  0.76506110
100%  3.0837762  2.89445426  3.23701556  3.084539130  3.06543839  3.07470137
            [,7]        [,8]        [,9]       [,10]
0%   -2.78486552 -3.44680297 -3.27408224 -2.87007187
25%  -0.69813726 -0.62989631 -0.68281294 -0.61806994
50%  -0.02589364  0.03913355 -0.04370316  0.05563488
75%   0.66519651  0.74677634  0.64744094  0.74664823
100%  3.13266873  4.64939039  2.68895458  3.16797447
> df <- as.data.frame(t(arr))
> df
          0%        25%          50%       75%     100%
1  -3.357431 -0.6610031  0.057661700 0.7584004 3.083776
2  -3.097050 -0.6850623  0.058998490 0.6787953 2.894454
3  -2.850096 -0.6689111 -0.012355714 0.6933582 3.237016
4  -3.140182 -0.6817858  0.007168725 0.6591214 3.084539
5  -2.942354 -0.6698521  0.071399769 0.7285408 3.065438
6  -3.243349 -0.6043932  0.075116334 0.7650611 3.074701
7  -2.784866 -0.6981373 -0.025893640 0.6651965 3.132669
8  -3.446803 -0.6298963  0.039133547 0.7467763 4.649390
9  -3.274082 -0.6828129 -0.043703156 0.6474409 2.688955
10 -2.870072 -0.6180699  0.055634885 0.7466482 3.167974
> class(df)
[1] "data.frame"
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453