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:
- 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?