无法让 dplyr::ungroup 取消组合

dat*_*ter 1 r dplyr

我确定我在这里遗漏了一些明显的东西。我想将行号添加到汇总表中,但 dplyr 根据我之前的分组循环/重复行号。我以为我已经取消了它的分组,但显然我遗漏了一些东西。我希望索引中的每一行都有一个唯一的行号。

#Make sample data
    Species <- c("A", "B", "C","D")
    Animal <- c(1:100)
    Day <-c(1:10)
    P <- sample(1:100, 300, replace = TRUE)
    F <- sample(1:100, 300, replace = TRUE)
    C <- sample(1:100, 300, replace = TRUE)
    df <- data.frame(Species,Animal,Day, P, F, C)

 #Summarize by columns
    by_day <- df %>%
      group_by(Species, Animal, Day) %>%
      summarize(ptot = sum(P), ftot = sum(F), ctot = sum(C))

 #Here's where I suspect the problem lies    
    ungroup(by_day)

 #This line produces repeating id numbers, where as I want each line to have a unique one.
    indexed <- mutate(by_day, id = row_number())
Run Code Online (Sandbox Code Playgroud)

Psi*_*dom 5

您似乎认为ungroupby_day就地修改,但事实并非如此;您需要确保将未分组的数据框传递给mutate

mutate(ungroup(by_day), id = row_number())

# A tibble: 100 x 7
#   Species Animal   Day  ptot  ftot  ctot    id
#    <fctr>  <int> <int> <int> <int> <int> <int>
# 1       A      1     1   266   262    45     1
# 2       A      5     5    84   201   159     2
# 3       A      9     9   141   149   244     3
# 4       A     13     3    94   142   157     4
# 5       A     17     7   188   138   142     5
Run Code Online (Sandbox Code Playgroud)

1)之前ungroup

by_day
# A tibble: 100 x 6
# Groups:   Species, Animal [?]        <<<<<<<<<<<<<<<<<<<<
#   Species Animal   Day  ptot  ftot  ctot
Run Code Online (Sandbox Code Playgroud)

2)ungroup返回未分组的数据帧:

ungroup(by_day)
# A tibble: 100 x 6
#   Species Animal   Day  ptot  ftot  ctot
Run Code Online (Sandbox Code Playgroud)

3)但它没有修改by_day,它仍然是分组的

by_day
# A tibble: 100 x 6
# Groups:   Species, Animal [?]        <<<<<<<<<<<<<<<<<<<<
#   Species Animal   Day  ptot  ftot  ctot
Run Code Online (Sandbox Code Playgroud)