Summary Statistics in a table format in R R中以表格格式呈现的汇总统计信息

huangapple go评论49阅读模式
英文:

Summary Statistics in a table format in R

问题

我可以创建一个关于观测数量的汇总统计表。

英文:

How can I create a table of summary statistics for the number of observations

答案1

得分: 1

使用 sapply 遍历每一列并进行计算。

sapply(mtcars, \(x) c(n = length(x), mean = mean(x), sd = sd(x),
                      median = median(x), min = min(x), max = max(x),
                      nMissing = sum(is.na(x)) ))
#               mpg       cyl     disp        hp       drat         wt      qsec
#n        32.000000 32.000000  32.0000  32.00000 32.0000000 32.0000000 32.000000
#mean     20.090625  6.187500 230.7219 146.68750  3.5965625  3.2172500 17.848750
#sd        6.026948  1.785922 123.9387  68.56287  0.5346787  0.9784574  1.786943
#median   19.200000  6.000000 196.3000 123.00000  3.6950000  3.3250000 17.710000
#min      10.400000  4.000000  71.1000  52.00000  2.7600000  1.5130000 14.500000
#max      33.900000  8.000000 472.0000 335.00000  4.9300000  5.4240000 22.900000
#nMissing  0.000000  0.000000   0.0000   0.00000  0.0000000  0.0000000  0.000000
#                 vs         am       gear    carb
#n        32.0000000 32.0000000 32.0000000 32.0000
#mean      0.4375000  0.4062500  3.6875000  2.8125
#sd        0.5040161  0.4989909  0.7378041  1.6152
#median    0.0000000  0.0000000  4.0000000  2.0000
#min       0.0000000  0.0000000  3.0000000  1.0000
#max       1.0000000  1.0000000  5.0000000  8.0000
#nMissing  0.0000000  0.0000000  0.0000000  0.0000

这是使用 sapply 进行计算并生成的数据表格。

英文:

Use sapply to go to each column and do the calculations.

sapply(mtcars, \(x) c(n = length(x), mean = mean(x), sd = sd(x),
                      median = median(x), min = min(x), max = max(x),
                      nMissing = sum(is.na(x)) ))
#               mpg       cyl     disp        hp       drat         wt      qsec
#n        32.000000 32.000000  32.0000  32.00000 32.0000000 32.0000000 32.000000
#mean     20.090625  6.187500 230.7219 146.68750  3.5965625  3.2172500 17.848750
#sd        6.026948  1.785922 123.9387  68.56287  0.5346787  0.9784574  1.786943
#median   19.200000  6.000000 196.3000 123.00000  3.6950000  3.3250000 17.710000
#min      10.400000  4.000000  71.1000  52.00000  2.7600000  1.5130000 14.500000
#max      33.900000  8.000000 472.0000 335.00000  4.9300000  5.4240000 22.900000
#nMissing  0.000000  0.000000   0.0000   0.00000  0.0000000  0.0000000  0.000000
#                 vs         am       gear    carb
#n        32.0000000 32.0000000 32.0000000 32.0000
#mean      0.4375000  0.4062500  3.6875000  2.8125
#sd        0.5040161  0.4989909  0.7378041  1.6152
#median    0.0000000  0.0000000  4.0000000  2.0000
#min       0.0000000  0.0000000  3.0000000  1.0000
#max       1.0000000  1.0000000  5.0000000  8.0000
#nMissing  0.0000000  0.0000000  0.0000000  0.0000

答案2

得分: 0

summary() 可以帮助您获取数据的均值、中位数、最小值和最大值。

length() 可以获取观测值的数量。

sd() 用于计算标准差。

sum(is.na(您的数据)) 用于计算数据中缺失观测值的数量。

英文:

You can use several function to complete that:

summary() can help you get mean, median, min and max of your data

length() can make you get the number of observations

sd() to calculate sd

sum(is.na(your data)) for number of missing observations for data

huangapple
  • 本文由 发表于 2023年4月20日 06:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定