【问题标题】:Apply custom function to each subset of a data frame and result a dataframe将自定义函数应用于数据框的每个子集并生成数据框
【发布时间】:2016-05-21 04:38:22
【问题描述】:

这里可能会被多次询问,但由于我的函数返回数据框,因此我无法将其与任何人联系起来。

我有我的自定义函数,它构建模型并输出一个数据框,其中一列中的斜率(coeff2),另一列中的截距(coeff1),一列中的输入记录数等。理想情况下,我在函数中构建自己的数据框并从函数中输出。现在我想根据一列对我的输入数据框进行子集化并在其上应用我的函数。

示例:-

f.get_reg <- function(df) {
  linear.model <-lm(df$DM ~ df$FW,)
  N <- length(df$DM)
  slope <- coef(linear.model)[2]
  intercept <- coef(linear.model)[1]
  S <- summary(linear.model)$sigma
  df.out <- data.frame (N,slope, intercept, S)
  return (df.out)
}



sample_id     FW       DM  StdDev_DM Median_DM Count  X90 X60 crit Z.scores
     6724 116.39 16.20690    0.9560414   16.0293    60 3.35 3.2  3.2        1
     6724 116.39 16.20690    0.9560414   16.0293    60 3.35 3.2  3.2        1
     6724 110.24 16.73077    0.9560414   16.0293    60 3.35 3.2  3.2        1
     6728 110.24 16.73077    0.9560414   16.0293    60 3.35 3.2  3.2        1
     6728 112.81 16.15542    0.9560414   16.0293    60 3.35 3.2  3.2        1
     6728 112.81 16.15542    0.9560414   16.0293    60 3.35 3.2  3.2        1

现在我想将我的函数应用于 sample_ids 的每个唯一子集,并仅输出一个数据帧,其中一个记录作为每个子集的输出。

【问题讨论】:

    标签: r dplyr plyr tapply


    【解决方案1】:

    dplyr

    您可以在dplyr 中使用do

    library(dplyr)
    df %>%
        group_by(sample_id) %>%
        do(f.get_reg(.))
    

    这给出了:

      sample_id     N       slope intercept            S
          (int) (int)       (dbl)     (dbl)        (dbl)
    1      6724     3 -0.08518211  26.12125 7.716050e-15
    2      6728     3 -0.22387160  41.41037 5.551115e-17
    

    data.table

    data.table 中使用.SD

    library(data.table)
    
    df <- data.table(df)
    df[,f.get_reg(.SD),sample_id]
    

    结果相同:

       sample_id N       slope intercept            S
    1:      6724 3 -0.08518211  26.12125 7.716050e-15
    2:      6728 3 -0.22387160  41.41037 5.551115e-17
    

    基础 R

    使用by

    resultList <- by(df,df$sample_id,f.get_reg)
    sample_id <- names(resultList)
    result <- do.call(rbind,resultList)
    result$sample_id <- sample_id
    rownames(result) <- NULL
    

    这给出了:

      N       slope intercept            S sample_id
    1 3 -0.08518211  26.12125 7.716050e-15      6724
    2 3 -0.22387160  41.41037 5.551115e-17      6728
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 2017-01-28
      • 2020-10-28
      • 1970-01-01
      • 2013-11-19
      相关资源
      最近更新 更多