如何获取列表中每一列的摘要 [英] How to get summary for each column of a list

查看:42
本文介绍了如何获取列表中每一列的摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们以一个名副其实的 cars 为例.汽车有两列 cars $ speed cars $ dist .

Lets take a veriable cars as an example. Cars has two columns cars$speed, cars$dist.

我想编写一个函数,该函数将一步一步地为veriable的每一列(在本例中为cars)打印.看起来像:

I want to write a function that will print in one step summary for each column of a veriable(in this case cars). It would look like:

f<-function(x){
#do some stuff
}

结果:

name of first column:
 Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
4.0    12.0    15.0    15.4    19.0    25.0 
name of second column:
     Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
       2.00   26.00   36.00   42.98   56.00  120.00 

我该怎么做?

推荐答案

如果只需要对分位数和均值,中位数进行汇总,则只需在数据框架上调用 summary().它将为您提供每列的摘要.如果要调用其他功能...

If all you want is a summary of quantiles and mean, median, then just call summary() on your data frame. It will give you a summary for each column. If you want to call other functions...

为此,有一个很棒的软件包, dplyr .看一下 summarise_each() summarise().

There's a great package for that, dplyr. Take a look at summarise_each() and summarise().

假设您要查找每列的均值并将输出作为其自己的数据框:

Say you want to find the mean of each column and have the output be its own data frame:

install.packages('dplyr')
library(dplyr)
new_df <- summarise_each(cars, funs(mean))

## Subsetting to only summarize specific columns
new_df <- summarise_each(cars[, c('speed', 'dist')], funs(mean))

您还可以使用 group_by()函数基于数据中的不同组来计算汇总.您没有问这个,所以我就在这里停止.

You can also compute summaries based on different groups in your data, using the group_by() function. You didn't ask about that so I'll just stop here.

这篇关于如何获取列表中每一列的摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆