【问题标题】:R-Dataframe : mean by row on all the DF with conditionnal on other columnR-Dataframe:在所有 DF 上按行表示,以其他列为条件
【发布时间】:2020-02-04 01:50:44
【问题描述】:

我从我想要的代码开始(都是用例子写的)

df <- data.frame(comp = c(10, 12, 14, 17, 17),
                 val = c(0, 5, 10, 15, 20),
                 cond_inf = c(8, 9.6, 11.2, 13.6, 13.6),
                 cond_sup = c(12, 14.4, 16.8, 20.4, 20.4),
                 mean_cond_text1 = c("Average of VAL lines whose COMP is between 8 12", 
                                     "Average of VAL lines whose COMP is between 9.6 14.4",
                                     "Average of VAL lines whose COMP is between 11.2 16.8",
                                     "Average of VAL lines whose COMP is between 13.6 20.4", 
                                     "Average of VAL lines whose COMP is between 13.6 20.4"),
                 mean_cond_text2 = c("(val_row1+val_row2)/2", "(val_row1+val_row2+val_row3)/3", "(val_row2+val_row3)/2", "(val_row3+val_row4+val_row5)/2", "(val_row3+val_row4+val_row5)/2)"),
                 mean_cond_text3 = c("(0+5)/2", "(0+5+10)/3", "(5+10)/2", "(10+15+20)/3", "(10+15+20)/3)"),
                 mean_cond_num = c((0+5)/2, (0+5+10)/3, (5+10)/2, (10+15+20)/3, (10+15+20)/3))

我希望在数据帧的每一行上计算列 VAL 的平均值,对于 COMP 比较值包含在我计算平均值的行的 COND_INF - COND_SUP 间隔中的所有行。所以我的数据框的每一行都有一个平均值来计算。

在Dataframe中,每行4列一直填写

COMP = 应用条件以在计算平均值时考虑或不考虑行的列

VAL = 如果考虑线,将用于计算平均值的值

COND_INF = 下限(COMP 的-20%),其 COMP 必须大于等于考虑

COND_SUP = 上限(+ COMP 的 20%),其 COMP 必须为 lower-equal 才能考虑

谢谢你的帮助,我迷路了……

【问题讨论】:

    标签: r dataframe conditional-statements mean


    【解决方案1】:

    不完全确定所需的方法,但这似乎与您可能正在寻找的方法接近。

    查看您的数据框,不清楚第 3 行到第 5 行是如何计算的。例如,第 3 行的 comp 为 14。这应该在第 2-5 行的范围内,而不仅仅是第 2 行和第 3 行?第 4 行和第 5 行的范围是 (13.6, 20.4),应该包含在计算 comp 的 14 中吗?对于第 4 行和第 5 行,我也得到了不同的平均值。

    如果我的理解不正确,请告诉我。根据我目前的印象,这是一种方法。我怀疑使用data.tablesqldf 等有更好的替代方法。

    df <- data.frame(comp = c(10, 12, 14, 17, 17),
                     val = c(0, 5, 10, 15, 20),
                     cond_inf = c(8, 9.6, 11.2, 13.6, 13.6),
                     cond_sup = c(12, 14.4, 16.8, 20.4, 20.4))
    
    library(dplyr)
    
    # Add index for row number
    df$idx <- seq(1, nrow(df))
    
    # Split dataframe into comp and index and look up table with values and range
    df1 <- df[c(1,5)]
    df2 <- df[2:4]
    
    # Expand grid to get multiple combinations and filter to those where comp in range 
    expand_grid(df1, df2) %>%
      filter(between(comp, cond_inf, cond_sup)) %>%
      group_by(idx) %>%
      mutate(mean_cond_num = mean(val)) %>%
      right_join(df)
    
       comp   idx   val cond_inf cond_sup mean_cond_num
      <dbl> <int> <dbl>    <dbl>    <dbl>         <dbl>
    1    10     1     0      8       12             2.5
    2    12     2     5      9.6     14.4           5  
    3    14     3    10     11.2     16.8          12.5
    4    17     4    15     13.6     20.4          17.5
    5    17     5    20     13.6     20.4          17.5
    

    【讨论】:

      【解决方案2】:

      感谢您的帮助。 根据你的想法,我做到了

      df <- data.frame(comp = c(10, 12, 14, 17, 17),
                       val = c(0, 5, 10, 15, 20),
                       cond_inf = c(8, 9.6, 11.2, 13.6, 13.6),
                       cond_sup = c(12, 14.4, 16.8, 20.4, 20.4),
                       mean_cond_num = c((0+5)/2, (0+5+10)/3, (5+10)/2, (10+15+20)/3, (10+15+20)/3))
      
      df$id <- seq(1, nrow(df))
      df2 <- sqldf("SELECT a.*, b.val as val2, b.cond_inf as cond_inf2, b.cond_sup as cond_sup2
             FROM df as a, df as b
             where a.cond_inf <= b.comp
                and a.cond_sup >= b.comp")
      
      df3 <- df2 %>%
        group_by(id, mean_cond_num) %>%
        summarise(mmoy = mean(val2))
      

      它有效,我必须尝试根据我的真实数据,计算时间是否可以。 如果没问题,我会回来解决的。 谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-25
        • 2021-01-04
        • 1970-01-01
        • 2023-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多