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

Farrel Buchinsky fjbuch at gmail.com
Fri May 5 18:08:55 CEST 2006


"Uwe Ligges" <ligges at statistik.uni-dortmund.de> wrote in message 
news:445B0F0A.3090005 at statistik.uni-dortmund.de...
>>
>> $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")

Yes. It works
Thank you
Resultdt<-lapply(PGWide[,8:13], tdt)
Puts the result of numerous tdt tests into Resultdt then
> t(sapply(Resultdt, "[[", "p.value"))
                 Chi-squared test         1         2
Genotype.4824685        0.7630246 1.0000000 1.0000000
Genotype.6482747        0.3173105 0.4243562 0.4243562
Genotype.4766200        0.1655179 0.2668457 0.2668457
Genotype.4767387        0.6547208 1.0000000 1.0000000
Genotype.4774028        1.0000000 1.0000000 1.0000000
Genotype.4786287        0.7630246 1.0000000 1.0000000

I do no know anything about the double square brackets in inverted commas. I 
know what indexing is as in a single square bracket but not the doubles. 
Where can I read more about it? The cherry on the top of this whole exercise 
would be if I could pass all the elements of the Resulttdt into one 
dataframe.

At the moment I would have to repeat the above command for "statistic" 
"p.value" "allele.frequencies" and then I would have to cbind() the whole 
lot.
So my question is how does one pass multiple arguments to [[

Incidentally, a question about mailing list etiquette. I picked up this 
thread from an interchange that had occurred a few months ago. Is that 
appropriate since it anchors the thread to what came before it when viewed 
on http://news.gmane.org/gmane.comp.lang.r.general however it may generate 
no interest since it does not come up as a new topic?




More information about the R-help mailing list