列表被添加到数据框

时间:2019-06-07 14:27:59

标签: r

为什么在这里将列表添加到我的数据框中?

这是我的数据框

df <- data.frame(ch = rep(1:10, each = 12), # care home id
                 year_id = rep(2018),
                 month_id = rep(1:12), # month using the system over the course of a year (1 = first month, 2 = second month...etc.)
                 totaladministrations = rbinom(n=120, size = 1000, prob = 0.6), # administrations that were scheduled to have been given in the month
                 missed = rbinom(n=120, size = 20, prob = 0.8), # administrations that weren't given in the month (these are bad!)
                 beds = rep(rbinom(n = 10, size = 60, prob = 0.6), each = 12), # number of beds in the care home
                 rating = rep(rbinom(n= 10, size = 4, prob = 0.5), each = 12)) # latest inspection rating (1. Inadequate, 2. Requires Improving, 3. Good, 4 Outstanding)


df <- arrange(df, df$ch, df$year_id, df$month_id)

str(df)

> str(df)
'data.frame':   120 obs. of  7 variables:
 $ ch                  : int  1 1 1 1 1 1 1 1 1 1 ...
 $ year_id             : num  2018 2018 2018 2018 2018 ...
 $ month_id            : int  1 2 3 4 5 6 7 8 9 10 ...
 $ totaladministrations: int  576 598 608 576 608 637 611 613 593 626 ...
 $ missed              : int  18 18 19 16 16 13 17 16 15 17 ...
 $ beds                : int  38 38 38 38 38 38 38 38 38 38 ...
 $ rating              : int  2 2 2 2 2 2 2 2 2 2 ...

到目前为止一切都很好。

我只想添加另一列来对ch组内的月号进行排序(在此示例中,这等于实际的month_id,但是忽略了这一点,我的实际数据是不同的),所以我正在使用:

df <- df %>% group_by(ch) %>% 
  mutate(sequential_month_counter = 1:n()) 

这似乎增加了一些我不太了解,不想要或不需要的东西,例如列表...

str(df)

> str(df)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 120 obs. of  8 variables:
 $ ch                      : int  1 1 1 1 1 1 1 1 1 1 ...
 $ year_id                 : num  2018 2018 2018 2018 2018 ...
 $ month_id                : int  1 2 3 4 5 6 7 8 9 10 ...
 $ totaladministrations    : int  601 590 593 599 615 611 628 587 604 600 ...
 $ missed                  : int  16 14 17 16 18 16 15 18 15 20 ...
 $ beds                    : int  35 35 35 35 35 35 35 35 35 35 ...
 $ rating                  : int  3 3 3 3 3 3 3 3 3 3 ...
 $ sequential_month_counter: int  1 2 3 4 5 6 7 8 9 10 ...
 - attr(*, "groups")=Classes ‘tbl_df’, ‘tbl’ and 'data.frame':  10 obs. of  2 variables:
  ..$ ch   : int  1 2 3 4 5 6 7 8 9 10
  ..$ .rows:List of 10
  .. ..$ : int  1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ : int  13 14 15 16 17 18 19 20 21 22 ...
  .. ..$ : int  25 26 27 28 29 30 31 32 33 34 ...
  .. ..$ : int  37 38 39 40 41 42 43 44 45 46 ...
  .. ..$ : int  49 50 51 52 53 54 55 56 57 58 ...
  .. ..$ : int  61 62 63 64 65 66 67 68 69 70 ...
  .. ..$ : int  73 74 75 76 77 78 79 80 81 82 ...
  .. ..$ : int  85 86 87 88 89 90 91 92 93 94 ...
  .. ..$ : int  97 98 99 100 101 102 103 104 105 106 ...
  .. ..$ : int  109 110 111 112 113 114 115 116 117 118 ...
  ..- attr(*, ".drop")= logi TRUE

这是怎么回事?我只想要一个数据框。为什么在$ sequential_month_counter: int 1 2 3 4 5 6 7 8 9 10 ...之后还有所有其他输出,更重要的是我可以忽略它,而只将其视为普通数据帧(我将在df上运行一些广义线性混合模型)?

1 个答案:

答案 0 :(得分:2)

属性"groups"dplyr存储执行group_by(ch)时添加的分组信息的位置。它没有任何伤害,如果您ungroup(),它会消失:

df %>% group_by(ch) %>% 
  mutate(sequential_month_counter = 1:n()) %>%
  ungroup %>%
  str
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 120 obs. of  8 variables:
#  $ ch                      : int  1 1 1 1 1 1 1 1 1 1 ...
#  $ year_id                 : num  2018 2018 2018 2018 2018 ...
#  $ month_id                : int  1 2 3 4 5 6 7 8 9 10 ...
#  $ totaladministrations    : int  575 597 579 605 582 599 577 604 630 632 ...
#  $ missed                  : int  18 16 16 18 18 11 10 13 17 16 ...
#  $ beds                    : int  33 33 33 33 33 33 33 33 33 33 ...
#  $ rating                  : int  3 3 3 3 3 3 3 3 3 3 ...
#  $ sequential_month_counter: int  1 2 3 4 5 6 7 8 9 10 ...

作为旁注,您应该在dplyr动词内使用裸列名称,而不要使用data$column。使用arrange并不重要,但是在分组操作中会导致错误。您应该养成使用arrange(df, ch, year_id, month_id)而不是arrange(df, df$ch, df$year_id, df$month_id)的习惯。