【问题标题】:Group_by then Filter and compute in RGroup_by 然后在 R 中过滤和计算
【发布时间】:2017-11-26 18:45:13
【问题描述】:

这是我的数据集 你可以从这个链接获取数据(如果不能,请告诉我) https://www.dropbox.com/s/1n9hpyhcniaghh5/table.csv?dl=0

     LABEL DATE TAU  TYPE   x    y    z
1      A    1    2    1   0.75   7   16
2      A    1    2    0   0.41   5   18
3      A    1    2    1   0.39   6   14
4      A    2    3    0   0.65   5   14
5      A    2    3    1   0.55   7   19
6      A    2    3    1   0.69   5   19
7      A    2    3    0   0.66   7   19
8      A    3    1    0   0.38   8   15
9      A    3    1    0   0.02   5   16
10     A    3    1    0   0.71   8   13
11     B    1    2    1   0.25   9   18
12     B    1    2    0   0.06   8   20
13     B    1    2    1   0.60   8   20
14     B    1    2    0   0.56   6   13
15     B    1    3    1   0.50   8   19
16     B    1    3    0   0.04   8   16
17     B    2    1    1   0.04   5   15
18     B    2    1    1   0.75   5   13
19     B    2    1    0   0.44   8   18
20     B    2    1    1   0.52   9   13

我想用多个条件按组过滤数据。条件是

  • TYPE 变量按组的每种类型(0,1)的行数必须 大于 1
  • 每种类型的行数必须相等 (例如:类型1的行数等于每个组的类型0的行数)

我尝试了很多次......最后我得到了这个代码和这个输出

table %>% group_by(label,date,tau,type) %>% filter(n()>1) %>% filter(length(type==1)==length(type==0))

# A tibble: 16 x 7
# Groups:   label, date, tau, type [7]
      LABEL  DATE  TAU  TYPE    x    y    z
     <fctr> <int> <int> <int> <dbl> <int> <int>
 1      A     1     2     1   0.75    7    16
 2      A     1     2     1   0.39    6    14
 3      A     2     3     0   0.65    5    14
 4      A     2     3     1   0.55    7    19
 5      A     2     3     1   0.69    5    19
 6      A     2     3     0   0.66    7    19
 7      A     3     1     0   0.38    8    15
 8      A     3     1     0   0.02    5    16
 9      A     3     1     0   0.71    8    13
10      B     1     2     1   0.25    9    18
11      B     1     2     0   0.06    8    20
12      B     1     2     1   0.60    8    20
13      B     1     2     0   0.56    6    13
14      B     2     1     1   0.04    5    15
15      B     2     1     1   0.75    5    13
16      B     2     1     1   0.52    9    13

我对这段代码得到的输出感到困惑。我已经去掉了不满足条件1的数据BUT不满足条件2的数据还在里面

我想要的结果就像下面这样

      LABEL  DATE  TAU  TYPE    x    y    z
     <fctr> <int> <int> <int> <dbl> <int> <int>
 3      A     2     3     0   0.65    5    14
 4      A     2     3     1   0.55    7    19
 5      A     2     3     1   0.69    5    19
 6      A     2     3     0   0.66    7    19
10      B     1     2     1   0.25    9    18
11      B     1     2     0   0.06    8    20
12      B     1     2     1   0.60    8    20
13      B     1     2     0   0.56    6    13

如果我想用下面的函数计算每一行的值,我该如何编码?就用mutate()这个函数??

f(x,y,z) = 2 * x + y - z / 3      if TYPE == 1
f(x,y,z) = 4 * x - y / 2 + z / 3  if TYPE == 0

我希望有人可以帮助我,感谢您的帮助!如果您需要提供任何其他信息,请告诉我~

【问题讨论】:

    标签: r filter group-by rows dplyr


    【解决方案1】:
    # example dataset
    df = read.table(text = "
    LABEL DATE TAU  TYPE   x    y    z
    1      A    1    2    1   0.75   7   16
    2      A    1    2    0   0.41   5   18
    3      A    1    2    1   0.39   6   14
    4      A    2    3    0   0.65   5   14
    5      A    2    3    1   0.55   7   19
    6      A    2    3    1   0.69   5   19
    7      A    2    3    0   0.66   7   19
    8      A    3    1    0   0.38   8   15
    9      A    3    1    0   0.02   5   16
    10     A    3    1    0   0.71   8   13
    11     B    1    2    1   0.25   9   18
    12     B    1    2    0   0.06   8   20
    13     B    1    2    1   0.60   8   20
    14     B    1    2    0   0.56   6   13
    15     B    1    3    1   0.50   8   19
    16     B    1    3    0   0.04   8   16
    17     B    2    1    1   0.04   5   15
    18     B    2    1    1   0.75   5   13
    19     B    2    1    0   0.44   8   18
    20     B    2    1    1   0.52   9   13
    ", header=T, stringsAsFactors=F)
    
    library(dplyr)
    library(tidyr)
    
    # function to use for each row
    # (assumes that type can be only 1 or 0)
    f = function(t,x,y,z) { ifelse(t == 1, 
                                   2 * x + y - z / 3, 
                                   4 * x - y / 2 + z / 3) }
    
    df %>%
      count(LABEL, DATE, TAU, TYPE) %>%                # count rows for each group (based on those combinations)
      filter(n > 1) %>%                                # keep groups with multiple rows
      mutate(TYPE = paste0("TYPE_",TYPE)) %>%          # update variable
      spread(TYPE, n, fill = 0) %>%                    # reshape data
      filter(TYPE_0 == TYPE_1) %>%                     # keep groups with equal number of rows for type 0 and 1
      select(LABEL, DATE, TAU) %>%                     # keep variables/groups of interest
      inner_join(df, by=c("LABEL", "DATE", "TAU")) %>% # join back info
      mutate(f_value = f(TYPE,x,y,z))                  # apply function
    
    # # A tibble: 8 x 8
    #   LABEL  DATE   TAU  TYPE     x     y     z    f_value
    #   <chr> <int> <int> <int> <dbl> <int> <int>      <dbl>
    # 1     A     2     3     0  0.65     5    14 4.76666667
    # 2     A     2     3     1  0.55     7    19 1.76666667
    # 3     A     2     3     1  0.69     5    19 0.04666667
    # 4     A     2     3     0  0.66     7    19 5.47333333
    # 5     B     1     2     1  0.25     9    18 3.50000000
    # 6     B     1     2     0  0.06     8    20 2.90666667
    # 7     B     1     2     1  0.60     8    20 2.53333333
    # 8     B     1     2     0  0.56     6    13 3.57333333
    

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 2020-05-10
      • 2020-02-19
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      相关资源
      最近更新 更多