[R] Outputing dataframe or vector from within a user defined function

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri May 5 10:38:34 CEST 2006


Farrel Buchinsky wrote:

> "Uwe Ligges" <ligges at statistik.uni-dortmund.de> wrote in message 
> news:43A7C167.4020104 at statistik.uni-dortmund.de...
> 
>>Simply speaking, the last value of a function is returned. You can also
>>explicitly call return(). You have to assign the value to a new variable
>>when you call the function. Example:
> 
> 
> I read your explanation and I half get it. However half getting it does not 
> help me when I am wanting to split apart a result that has multiple 
> components
> I am using dgc.genetics from David Clayton but I think a similar thing would 
> happen if one were doing any analysis whose output was multiple lines of 
> text. For instance the output from a two sample t-test.
> For example: How would one do multiple test and send the output to a 
> dataframe where every row was another instance of the test and each row 
> would have a column for the t value, another column for the df, another 
> column for the p-value, another column for the lower limit of the 95% CI, 
> another column for the upper limit of the CI and so on.
> 
> My preference is that you send me to website where this is explained such as 
> the r-wiki.(Looked for it there and could not find it)
> 
> Here is my example.
> 
>>tdt(Genotype.4766200)
> 
>         Transmission/disequilibrium test
> Data:   Genotype.4766200
> 
> Untransmitted allele frequencies, informative transmissions
> and exact P-values
> 
> Allele        Frequency     Transmitted   Untransmitted P-value
> 2                    0.1311          9                      4 
> 0.267
> Warning message:
> 1 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, as.allele.pair = TRUE, 
> allow.ambiguous = (parent ==
> 
> I can unclass it and get
> 
>>unclass(tdt(Genotype.4766200))
> 
> $statistic
> Chi-squared test
>         1.923077
> 
> $parameter
> DF
>  1
> 
> $exact
> [1] TRUE
> 
> $p.value
> Chi-squared test                1                2
>        0.1655179        0.2668457        0.2668457
> 
> $method
> [1] "Transmission/disequilibrium test"
> 
> $data.name
> [1] "Genotype.4766200"
> 
> $allele.frequencies
> Allele frequency
>         1         2
> 0.8688525 0.1311475
> 
> $informative.transmissions
>   Transmitted Untransmitted
> 1           4             9
> 2           9             4
> 
> Warning message:
> 1 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, as.allele.pair = TRUE, 
> allow.ambiguous = (parent ==
> 
> 
> So as you can see, even the different components of the output are still not 
> single values.
> How does one strip this to single numbers?
> I will be moving to lapply or sapply to run the analysis repeatedly over 
> 6000 times. There will be way too much output to eyeball.
>
> Another correspondent came up with a complicated way of sending printed 
> output to a file and then using various string pattern recognition 
> functions, to make R determine what was what. But that seems awfully 
> inelegant - turning a computer into a human reader with computer processing 
> speed.

No, simply access the components you want to get and put then into a 
data.frame.

Let's make a nonsense lm() example that is reproducible for me:

  result <- lapply(1:10, function(i){
    x <- rnorm(10)
    y <- rnorm(10)
    lm(y~x)
  })

result

Now you want to get the coefficients by applying the indexing function 
"[[" with argument "coefficients" (names of the corresposning element of 
the returned list) to the object result:

  sapply(result, "[[", "coefficients")


(in this particular case one would take the extractor function 
coefficients() instead, but this one was for demonstration purposes).

Best,
uwe


> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list