Edit source code

Hello friends,

I would like to know if it is possible to edit the source code of a function. For example, I would like to insert the standard deviation (SD) into the summmary function. The function shows just minumum, maximum, mean, median, first (25%) and third (75%) quartiles. Is it possible to change the source code?

Thank you very much!

You can create your own functions and overwrite the base functions, you could also fork a package modify the source and make your own version, but strictly speaking you can't modify the original one, unless you propose a change to the source code, but overwriting them has the same practical effect

1 Like

Could you explain to me how you could do this in the best way?

Another point is how can I see the actual source code of the summary function, for example? Is there a specific command that I can use in the R software to see this information?

Just define a function with the same name, and it will overwrite the base function.

summary <- function(x) {
    x
    }

Just type the name of the function in the console without parentheses, in the case of summary() this is a little more complex since it applies a different method depending on the class of object that is being passed to it, but for example, this is what you get for the data.frame method.

summary.data.frame
#> function (object, maxsum = 7L, digits = max(3L, getOption("digits") - 
#>     3L), ...) 
#> {
#>     ncw <- function(x) {
#>         z <- nchar(x, type = "w")
#>         if (any(na <- is.na(z))) {
#>             z[na] <- nchar(encodeString(z[na]), "b")
#>         }
#>         z
#>     }
#>     z <- lapply(X = as.list(object), FUN = summary, maxsum = maxsum, 
#>         digits = 12L, ...)
#>     nv <- length(object)
#>     nm <- names(object)
#>     lw <- numeric(nv)
#>     nr <- if (nv) 
#>         max(vapply(z, function(x) NROW(x) + !is.null(attr(x, 
#>             "NAs")), integer(1)))
#>     else 0
#>     for (i in seq_len(nv)) {
#>         sms <- z[[i]]
#>         if (is.matrix(sms)) {
#>             cn <- paste(nm[i], gsub("^ +", "", colnames(sms), 
#>                 useBytes = TRUE), sep = ".")
#>             tmp <- format(sms)
#>             if (nrow(sms) < nr) 
#>                 tmp <- rbind(tmp, matrix("", nr - nrow(sms), 
#>                   ncol(sms)))
#>             sms <- apply(tmp, 1L, function(x) paste(x, collapse = "  "))
#>             wid <- sapply(tmp[1L, ], nchar, type = "w")
#>             blanks <- paste(character(max(wid)), collapse = " ")
#>             wcn <- ncw(cn)
#>             pad0 <- floor((wid - wcn)/2)
#>             pad1 <- wid - wcn - pad0
#>             cn <- paste0(substring(blanks, 1L, pad0), cn, substring(blanks, 
#>                 1L, pad1))
#>             nm[i] <- paste(cn, collapse = "  ")
#>         }
#>         else {
#>             sms <- format(sms, digits = digits)
#>             lbs <- format(names(sms))
#>             sms <- paste0(lbs, ":", sms, "  ")
#>             lw[i] <- ncw(lbs[1L])
#>             length(sms) <- nr
#>         }
#>         z[[i]] <- sms
#>     }
#>     if (nv) {
#>         z <- unlist(z, use.names = TRUE)
#>         dim(z) <- c(nr, nv)
#>         if (anyNA(lw)) 
#>             warning("probably wrong encoding in names(.) of column ", 
#>                 paste(which(is.na(lw)), collapse = ", "))
#>         blanks <- paste(character(max(lw, na.rm = TRUE) + 2L), 
#>             collapse = " ")
#>         pad <- floor(lw - ncw(nm)/2)
#>         nm <- paste0(substring(blanks, 1, pad), nm)
#>         dimnames(z) <- list(rep.int("", nr), nm)
#>     }
#>     else {
#>         z <- character()
#>         dim(z) <- c(nr, nv)
#>     }
#>     attr(z, "class") <- c("table")
#>     z
#> }
#> <bytecode: 0x5644827429c8>
#> <environment: namespace:base>

I understand better now. Thanks for the answers.

One more thing, as I said earlier, the summary function shows minimum, maximum, mean, median, first (25%) and third (75%) quartiles. However, observing the summary.data.frame that you presented, I did not identify the code that addresses the aspects of media, median, first quartile, among others.
Would you have any suggestions for me?

Thanks again.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.