I'm writing a function whose output is a list of summary statistics for a data frame of unknown columns. For example, if my data frame had 3 columns, my list output needs to be as such:
col1
col1mean
(mean of column 1)
col1
col1median
(median of column 1)
col2
col2$mean
(mean of column 2)
col2
col2$median
(median of column2)
col3
col3mean
(mean of column 3)
col3
col3median
(median of column 3)
My question is, how can I return my list so that it will cycle through mean, median, and standard deviation for n number of columns?
One approach with lapply
:
# example data
set.seed(123)
dat <- data.frame(col1 = rnorm(10), col2 = rnorm(10), col3 = rnorm(10))
lapply(dat, function(x) list(mean = mean(x), median = median(x), sd = sd(x)))
The result:
$col1
$col1$mean
[1] 0.07462564
$col1$median
[1] -0.07983455
$col1$sd
[1] 0.9537841
$col2
$col2$mean
[1] 0.208622
$col2$median
[1] 0.3802926
$col2$sd
[1] 1.038073
$col3
$col3$mean
[1] -0.4245589
$col3$median
[1] -0.6769652
$col3$sd
[1] 0.9308092
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다