R 如何制作自定义项,以便对数据框中的所有变量进行描述性分析

R 如何制作自定义项,以便对数据框中的所有变量进行描述性分析,r,user-defined-functions,R,User Defined Functions,我有一个包含数百个变量的数据库,有不同的类:int、factor、logi、date、chr和num,但基本上它们都是factors,一个小示例如下: set.seed(123) dat <- data.frame( A = sample(1:10), B = rnorm(10, 25, 12), C = rnorm(10, 0, 2), H = sample(seq(as.Date('1999/01/01'), as.Dat

我有一个包含数百个变量的数据库,有不同的类:int、factor、logi、date、chr和num,但基本上它们都是factors,一个小示例如下:

set.seed(123)
dat <- data.frame(
        A = sample(1:10),
        B = rnorm(10, 25, 12),
        C = rnorm(10, 0, 2),
        H = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 10),
        f1 = sample(letters[1:3], 10, replace = TRUE),
        f2 = sample(letters[4:6], 10, replace = TRUE),
        Y = sample(c("yes", "no"), 10, replace = TRUE),
        W = sample(c("Male", "Female"), 10, replace = TRUE),
        Z = sample(c("true", "false"), 10, replace = TRUE))
MyFunction <-function(df, factor1, factor2, ...){
  # Descriptive analysis of the data frame, according to factor 1 level and factor 2
  #
  # Args:
  #    df: name of the data frame
  #    factor1: group (a, b, c)
  #    factor2: subgroup (d, e, f)
  #
  # Returns: 
  #  
  dat <- filter(df, f1 == factor1 & f2 == factor2) 
    apply(dat, 2, ifelse(is.factor == TRUE | is.character == TRUE, describe,
                      ifelse(is.integer == TRUE | is.numeric == TRUE, 
     summary, "other")))
}
MyFunction <-function(df, factor1, factor2, ...){
  dat <- filter(df, f1 == factor1 & f2 == factor2)
  apply(dat, 2, function(x) {
    if (is.factor(x) | is.character(x))
      describe(x)
    else if (is.numeric(x))
      summary(x)
    else "other"
  })
}
这是我想到的解决这项任务的最佳方法,但正如有人可以想出更好的解决方案或提供解决方案、建议或想法一样


非常感谢您的时间。

使用
apply
可能有点棘手。第三个参数需要是一个函数,它接受一列并对其进行一些处理。但是,您给出的表达式:

ifelse(is.factor == TRUE | is.character == TRUE, describe,
    ifelse(is.integer == TRUE | is.numeric == TRUE, summary,
      "other")))
这不是一个函数。它只是一个包含多个错误的表达式,因为例如,
是.factor
,无法与
TRUE
进行比较。需要首先将其应用于某个对象(即列),以获得一个布尔值,然后进行比较

相反,您将希望编写如下内容:

set.seed(123)
dat <- data.frame(
        A = sample(1:10),
        B = rnorm(10, 25, 12),
        C = rnorm(10, 0, 2),
        H = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 10),
        f1 = sample(letters[1:3], 10, replace = TRUE),
        f2 = sample(letters[4:6], 10, replace = TRUE),
        Y = sample(c("yes", "no"), 10, replace = TRUE),
        W = sample(c("Male", "Female"), 10, replace = TRUE),
        Z = sample(c("true", "false"), 10, replace = TRUE))
MyFunction <-function(df, factor1, factor2, ...){
  # Descriptive analysis of the data frame, according to factor 1 level and factor 2
  #
  # Args:
  #    df: name of the data frame
  #    factor1: group (a, b, c)
  #    factor2: subgroup (d, e, f)
  #
  # Returns: 
  #  
  dat <- filter(df, f1 == factor1 & f2 == factor2) 
    apply(dat, 2, ifelse(is.factor == TRUE | is.character == TRUE, describe,
                      ifelse(is.integer == TRUE | is.numeric == TRUE, 
     summary, "other")))
}
MyFunction <-function(df, factor1, factor2, ...){
  dat <- filter(df, f1 == factor1 & f2 == factor2)
  apply(dat, 2, function(x) {
    if (is.factor(x) | is.character(x))
      describe(x)
    else if (is.numeric(x))
      summary(x)
    else "other"
  })
}
更接近你要找的东西

不幸的是,还有一个问题<代码>应用是用于矩阵的传统函数。当它与数据帧一起使用时,它有一个坏习惯,就是将其转换为矩阵(所有元素的类型都相同)。在本例中,它将整个内容转换为字符矩阵,甚至数字值也被视为字符/因子

幸运的是,还有另一个遗留函数,称为
lappy
,它适用于数据帧,并且不会将列转换为不同的类型。所以,如果你写:

MyFunction <-function(df, factor1, factor2, ...){
  dat <- filter(df, f1 == factor1 & f2 == factor2)
  lapply(dat, function(x) {
    if (is.factor(x) || is.character(x))
      describe(x)
    else if (is.numeric(x)) {
      summary(x)
    } else "other"
  })
}

如果您使用的是
library(tidyverse)
,那么您可以用
map
替换
lappy
——它们在这里做同样的事情,但是
map
是更现代的功能。

如果您可以添加一个示例,说明您希望输出也是什么样子,那就好了。