簡體   English   中英

Group_by然后在R中過濾和計算

[英]Group_by then Filter and compute in R

這是我的數據集。您可以從此鏈接獲取數據(如果不能,請通知我) 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

我想按多個條件按組過濾數據。 條件是

  • 組的每個類型(0,1)的TYPE變量的行數必須大於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,但其中的數據並沒有滿足條件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

希望有人能幫助我,感謝您的幫助! 如果您需要提供其他信息,請告訴我〜

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM