[R] function customization

maxbre mbressan at arpa.veneto.it
Fri Nov 16 09:55:02 CET 2012


Given my reproducible example:

new.ex<-structure(list(TEC = c(0.21, 0.077, 0.06, 0.033, 0.014, 0.007, 
0.21, 0.077, 0.01, 0.033, 0.05, 0.014), LR = c(FALSE, FALSE, 
TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE
), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("1", "2"), class = "factor")), .Names = c("TEC", 
"LR", "group"), row.names = c(NA, -12L), class = "data.frame")

And this few lines of code:

library(NADA)

out<-with(new.ex,
          cenfit(TEC, LR, group)
                    )
out

giving the following output:

        n n.cen median       mean         sd
group=1 6     2  0.033 0.05827778 0.08357853
group=2 6     3  0.033 0.06983333 0.07925407

I would like to add one more result for each group to the above output,
namely “sum”, computed as the product of “n” times “mean”

This is pretty much similar (a slight variation) to a question I posted
earlier in:
http://r.789695.n4.nabble.com/How-to-modify-a-S4-function-in-the-package-NADA-td4649586.html

But in this case I have some problems in modifying the cenfit() function
dealing with group as a factor 

My objective is to modify the original function cenfit() so that to get also
the computation of “sum” as product of “n” times “mean”

For some reasons I can not properly understand, I’m not able to successfully
modify my earlier attempt (which was not accounting for groups)

mycenfit <- function(x) { 
 s = summary(x) 
 c(n = nrow(s), n.cen = nrow(s) - sum(s$n.event), median = median(x), 
 mean = mean(x)[["mean"]], sd = sd(x), sum=mean(x)[["mean"]]*length(x)) 
} 

how to change it in order to properly deal with groups?

Thank you for any help

max



--
View this message in context: http://r.789695.n4.nabble.com/function-customization-tp4649711.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list