Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a function in R having these input and outputs

I want to create a function in R which summarize a vector of numbers into a dataframe representing a frequency table with k classes.

The Input are:

  • x : a vector of numeric data of any length
  • k : an integer greater than 1 that represents the number of equal width classes dividing the support of x

The outputs are:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • DF: a dataframe with three columns: min bound, max bound and frequency and k rows
  • d_m : numeric value indicating the mean of the input data
  • d_sd : numeric value indicating the standard deviation of the input data
  • m_error : a numeric value indicating the difference between d_m and d_s
  • df_m : numeric value indicating the mean computed through the tabled data
  • df_sd : numeric value indicating the standard deviation computed through the tabled data
  • sd_err: numeric value indicating the difference between d_sd and df_sd

I’m using R so the function follow the syntax:

namefunction <- function(...){...}

Thank you in advance guys for any sort of help !

>Solution :

Dividing the support into equal width fields and counting the data in those, that is called a histogram and you could try to use the hist function for that.

The mean of some data can be computed via the mean command and the standard deviation via sd.

x_bsp <- rnorm(100)


namefunction <- function(x, k){
    h <- hist(x_bsp, breaks = k, plot = FALSE)
    return(h)
}

namefunction(x_bsp, 5)

might be a start. Does any of this help to produce a more focused question?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading