[R] function customization

Uwe Ligges ligges at statistik.tu-dortmund.de
Mon Nov 19 11:25:20 CET 2012



On 16.11.2012 09:55, maxbre wrote:
> 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


The output is produces from the survfit object by
  survival:::print.survfit
just adapt the code for your need and add the columns you like.

Uwe Ligges



> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list