[R] closest match in R to c-like struct?

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat May 1 19:56:53 CEST 2010

On 01-May-10 16:58:49, Giovanni Azua wrote:
> On May 1, 2010, at 6:48 PM, steven mosher wrote:
>> I was talking with another guy on the list about this very topic.
>> A simple example would help.
>> first a sample C struct, and then how one would do the equivalent in
>> R.
>> In the end i suppose one want to do a an 'array' of these structs, or
>> list
>> of the structs.
> Or like in my use-case ... I needed a c-like struct to define the type
> for aggregating the data to return from a function. 
> Best regards,
> Giovanni

Assuming that I understand what you want, this is straightforward
and can be found throughout the many functions available in R.
The general form is:

  myfunction <- function(...){
    <code to compute objects A1, A2, ... , An>
    list(valA1=A1, valA2=A2, ... , valAn=An)

and then a call like

  myresults <- myfunction(...)

will create a list "myresults" with compnents "valA1", ... ,"valAn"
which you can access as desired on the lines of


As a simple example, the following is a function which explores
by simulation the power of the Fisher Exact Test for comparing
two proportions in a 2x2 table:

  power.fisher.test <- function(p1,p2,n1,n2,alpha=0.05,nsim=100){
    y1 <- rbinom(nsim,size=n1,prob=p1)
    y2 <- rbinom(nsim,size=n2,prob=p2)
    y <- cbind(y1,n1-y1,y2,n2-y2)
    p.value <- rep(0,nsim)
    for (i in 1:nsim)
      p.value[i] <- fisher.test(matrix(y[i,],2,2))$p.value
    list(Pwr=mean(p.value < alpha),SE.Pwr=sd(p.value < alpha)/sqrt(nsim))

So, given two binomials B(n1,p1) and B(n2,p2), what would be the
power of the Fisher test to detect that p1 was different from p2,
at given significance level alpha? This is investigated by repeating,
nsim times:
  sample from Bin(n1,p1), sample from Bin(n2.p2)
  do a Fisher test and get its P-value; store it
    in a vector p.value of length nsim
and then finally:
  estimate the power as the proportion Pwr of the nsim cases
    in which the P-value was less than alpha
  get the SE of this estimate
  return these two values as components Pwr and SE.Pwr of a list
As it happens, here each component of the resulting list is of
the same type (a single number); but in a different computation
each component (and of course there could be more than two)
could be anything -- even another list. So you can have lists
of lists ... !

Thus, instead of the simple returned list above:

  list(Pwr=mean(p.value < alpha),
       SE.Pwr=sd(p.value < alpha)/sqrt(nsim))

you could have

       Pwr=mean(p.value < alpha),
       SE.Pwr=sd(p.value < alpha)/sqrt(nsim))

thus also returning the details of the Binomials for which the
simulation was carried out. You could access these all together as:


or separately as


or even


E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-May-10                                       Time: 18:56:50
------------------------------ XFMail ------------------------------

More information about the R-help mailing list