简体   繁体   English

在 R 中跨列应用函数列表以填写表格

[英]Apply list of functions across columns in R to fill in table

I have a dataframe in R, let's call it df , which I would like to analyse in terms of mean, median, standard deviation, IQR etc column-wise.我在 R 中有一个 dataframe,我们称之为df ,我想按均值、中值、标准差、IQR 等按列进行分析。 I have prepared succinct functions (where it's not just mean or sd ) which can take a vector as input and output, say, the IQR or coefficient of variance.我准备了简洁的函数(不仅仅是meansd ),它可以将向量作为输入和 output,比如 IQR 或方差系数。 Now, if I want to apply any of these across the attributes (columns), I could use IQRs <- apply(df,2,IQR) for example.现在,如果我想在属性(列)中应用这些中的任何一个,我可以使用IQRs <- apply(df,2,IQR)例如。

My question is, how can I apply multiple of these functions together (really, I want to chain them all together), so as to fill in a table where there will be one column for the attributes and then one column per function (ie Means will be one column, IQRs will be one column), and the different attributes of the data-frame (which were columns in df ) will be rows of this table (listed in the first column)?我的问题是,我怎样才能将多个这些功能一起应用(真的,我想将它们全部链接在一起),以便填写一个表格,其中将有一列用于属性,然后每个 function 有一列(即意味着将是一列,IQR 将是一列),数据框的不同属性(在df中是列)将是该表的行(在第一列中列出)?

Suppose your data looked like this:假设您的数据如下所示:

set.seed(69)
df <- data.frame(A = rnorm(5), B = rnorm(5), C = rnorm(5))

And your function names were like this:你的 function 名字是这样的:

funcs <- c("mean", "median", "sd", "var", "min", "max")

Then you can use an apply inside an lapply like this:然后你可以像这样在lapply中使用apply

as.data.frame(setNames(lapply(funcs, function(f) apply(df, 2, as.name(f))), funcs))
#>        mean     median        sd       var       min       max
#> A -0.3546864 -0.3348139 0.5948611 0.3538597 -0.949889 0.3743156
#> B -0.2016318 -0.9039467 1.4092795 1.9860687 -1.571073 1.4440935
#> C -0.3537707 -0.1691765 0.7955558 0.6329090 -1.311374 0.4149940

You can use tidyr::gather and dplyr::summarize :您可以使用tidyr::gatherdplyr::summarize

# Toy data
df <- data.frame(x = 1:10, y = 11:20)

# Libs
library(tidyverse)

# Code
df %>% 
    gather(var, val) %>% 
    group_by(var) %>% 
    summarize(med = median(val), mean = mean(val), iqr = IQR(val))

Output: Output:

# A tibble: 2 x 4
  var     med  mean   iqr
  <chr> <dbl> <dbl> <dbl>
1 x       5.5   5.5   4.5
2 y      15.5  15.5   4.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM