I want to write a function that takes a data frame as an input and for each numeric variable in the data frame the function returns to the user the variables’ mean, median, and inter-quartile range in the form of a list.
The data frame is below:
'data.frame': 271 obs. of 6 variables:
$ sample.id: int 1 2 4 5 6 7 8 9 12 13 ...
$ zip : int 48504 48507 48504 48507 48505 48507 48507 48503 48507 48505 ...
$ ward : int 6 9 1 8 3 9 9 5 9 3 ...
$ Pb1 : num 0.344 8.133 1.111 8.007 1.951 ...
$ Pb2 : num 0.226 10.77 0.11 7.446 0.048 ...
$ Pb3 : num 0.145 2.761 0.123 3.384 0.035 ...
The output should be like:
$Pb1
Mean Median IQR
10.76687 3.56400 7.75100
$Pb2
Mean Median IQR
10.43467 1.40000 4.50100
$Pb3
Mean Median IQR
3.701434 0.839000 2.429500
Here is my code:
df.numeric.summary <- function(data) {
for (i in 1:ncol(data)) {
if (is.numeric(data[[i]]) == TRUE) {
variable_mean <- mean(data[[i]])
variable_median <- median(data[[i]])
variable_IQR <- IQR(data[[i]])
variable_data <- data.frame(Mean = variable_mean, Median = variable_median, IQR = variable_IQR)
}
}
return(variable_data)
}
My code only result in Pb3
, I think I could not use for
statement, but how could I get three variables' value? Also, how to return the result into a list?