我确定我在这里遗漏了一些明显的东西。我想将rownumbers添加到汇总表中,但是dplyr会循环/重复我之前分组的rownumbers。我以为我已经解开了它,但显然我错过了一些东西。我希望索引的每一行都有一个独特的rownumber。
#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())
答案 0 :(得分:3)
您似乎认为ungroup
会修改by_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
:
ungroup
1)在by_day
# A tibble: 100 x 6
# Groups: Species, Animal [?] <<<<<<<<<<<<<<<<<<<<
# Species Animal Day ptot ftot ctot
之前:
ungroup
2)ungroup(by_day)
# A tibble: 100 x 6
# Species Animal Day ptot ftot ctot
返回未分组的数据框:
by_day
3)但它不会修改by_day
# A tibble: 100 x 6
# Groups: Species, Animal [?] <<<<<<<<<<<<<<<<<<<<
# Species Animal Day ptot ftot ctot
,它仍然是分组
$('body').on('submit', 'form[data-confirm-dialog-text]', (e) ->
form = $(this)
e.preventDefault()
$.show_dialog(form.data('confirm-dialog-title'), form.data('confirm-dialog-text'), form.data('ok-text'), form.data('cancel-text'), (->
form.off('submit').submit()
))
)
答案 1 :(得分:0)
如果您只是通过as.data.frame()
将分组数据框重新格式化为数据框,则它也会自动取消分组。