【发布时间】:2019-12-05 09:22:40
【问题描述】:
给定一个数据集,我们可以使用top_n 来限制我们在tidyverse 中返回的行数(即排序/排名)。我喜欢大多数tidyverse 操作的灵活性,因为它们在大多数情况下都可以撤消,即您可以回到开始的地方。
使用此处问题中的数据和可能的解决方案(我写的),我怎样才能最好地撤消top_n?。
数据:
df<-structure(list(milk = c(1L, 2L, 1L, 0L, 4L), bread = c(4L, 5L,
2L, 1L, 10L), juice = c(3L, 4L, 6L, 5L, 2L), honey = c(1L, 2L,
0L, 3L, 1L), eggs = c(4L, 4L, 7L, 3L, 5L), beef = c(2L, 3L, 0L,
1L, 8L)), class = "data.frame", row.names = c(NA, -5L))
代码:
df %>%
gather(key,value) %>%
group_by(key) %>%
summarise(Sum=sum(value)) %>%
arrange(desc(Sum)) %>%
top_n(3,Sum) %>%
ungroup()
上面给了我这个:
# A tibble: 3 x 2
key Sum
<chr> <int>
1 eggs 23
2 bread 22
3 juice 20
现在我将(学习如何)做的是返回原始数据集而不删除代码,即以编程方式从top_n 恢复:
我自然想到了spreading(res就是上面的结果):
spread(res,key,Sum)
# A tibble: 1 x 3
bread eggs juice
<int> <int> <int>
1 22 23 20
但是,如何从那个开始或撤消top_n 的替代解决方案只是无法想到(还)。我怎样才能最好地做到这一点?
【问题讨论】:
-
你的意思是如何从每组的总和到每组的所有原始个体值?
-
top_n不是filter,您无法撤消吗? -
你可以先
cols <- df %>% gather(key, value) %>% group_by(key) %>% summarise(Sum = sum(value)) %>% arrange(desc(Sum)) %>% top_n(3, Sum) %>% ungroup() %>% pull(key)然后df %>% select(one_of(cols))。 -
理论上也可以
df %>% select(one_of(df %>% gather(key, value) %>% group_by(key) %>% summarise(Sum = sum(value)) %>% arrange(desc(Sum)) %>% top_n(3, Sum) %>% ungroup() %>% pull(key))). -
甚至可能是
df %>% gather(key, value) %>% group_by(key) %>% summarise(Sum = sum(value), Values = list(value), Row = list(row_number())) %>% arrange(desc(Sum)) %>% top_n(3, Sum) %>% select(-Sum) %>% ungroup() %>% unnest() %>% spread(key, Values)。