pivot_longer 和 pivot_wider 的问题 [英] Issue with pivot_longer and pivot_wider
问题描述
我正在尝试使用 pivot_longer 和 pivot_wider 并且它在独立脚本中运行良好.但是一旦我在闪亮中使用它,我就会收到以下错误:
I am trying to use pivot_longer and pivot_wider and it works fine in a stand alone script. But as soon as I use this in shiny I get the following error:
Warning: Values in `value` are not uniquely identified; output will contain list-cols.
* Use `values_fn = list(value = list)` to suppress this warning.
* Use `values_fn = list(value = length)` to identify where the duplicates arise
* Use `values_fn = list(value = summary_fun)` to summarise duplicates
Warning: Error in : Can't cast `x` <list_of<double>> to `to` <double>.
数据
d1 <- tibble::tribble(
~Date, ~apple_count, ~apple_sale, ~banana_count, ~banana_sale, ~orange_count, ~orange_sale, ~peaches_count, ~peaches_sale, ~watermelon_count, ~watermelon_sale, ~strawberry_count, ~strawberry_sale,
"8/19/19", 10882.05495, 239575, 0, 0, 0, 0, 0, 0, 9643.600102, 630827, 0, 0,
"8/20/19", 516.29755, 11281, 0, 0, 0, 0, 0, 0, 6041.538067, 510219, 1694.44, 684210,
"8/21/19", 949.4084, 20150, 0, 0, 0, 0, 0, 0, 5371.758106, 565440, 9105.89, 3695182,
"8/22/19", 3950.5318, 88679, 0, 0, 0, 0, 0, 0, 5238.308826, 576678, 6179.47, 2501560,
"8/23/19", 2034.02055, 45672, 0, 0, 0, 0, 0, 0, 4994.43054, 518081, 7366.31, 2984563,
"8/24/19", 1770.50415, 38553, 0, 0, 0, 0, 0, 0, 5001.303585, 551733, 6275.43, 2531400,
)
下面是代码.
d1 %>%
pivot_longer(cols = -Date) %>%
separate(name, into=c('partner', 'parameter'), sep='_') %>%
pivot_wider(names_from = parameter, values_from = value) %>%
dplyr::group_by(partner) %>%
dplyr::summarise( Total_Count = sum(as.numeric(count)),
Total_Sale = sum(as.numeric(sale)))
可能导致此问题的原因.
What might be causing this issue.
刚刚更新了数据和代码.我使用了收集和传播,但现在转向更广泛和更长的时间.
Just updated the data and the code.I was using gather and spread but now moved to wider and longer.
推荐答案
您的示例数据实际上不会导致问题,因为它不包含任何重复的日期,我假设这是您的实际数据集中的问题,所以我已在示例数据中添加了重复的行:
Your example data doesn't actually cause the issue because it doesn't contain any duplicate dates, I'm assuming that's the issue in your actual dataset so I've added a duplicated row to the example data:
d1 <- tibble::tribble(
~Date, ~apple_count, ~apple_sale, ~banana_count, ~banana_sale, ~orange_count, ~orange_sale, ~peaches_count, ~peaches_sale, ~watermelon_count, ~watermelon_sale, ~strawberry_count, ~strawberry_sale,
"8/19/19", 10882.05495, 239575, 0, 0, 0, 0, 0, 0, 9643.600102, 630827, 0, 0,
"8/19/19", 10882.05495, 239575, 0, 0, 0, 0, 0, 0, 9643.600102, 630827, 0, 0,
"8/20/19", 516.29755, 11281, 0, 0, 0, 0, 0, 0, 6041.538067, 510219, 1694.44, 684210,
"8/21/19", 949.4084, 20150, 0, 0, 0, 0, 0, 0, 5371.758106, 565440, 9105.89, 3695182,
"8/22/19", 3950.5318, 88679, 0, 0, 0, 0, 0, 0, 5238.308826, 576678, 6179.47, 2501560,
"8/23/19", 2034.02055, 45672, 0, 0, 0, 0, 0, 0, 4994.43054, 518081, 7366.31, 2984563,
"8/24/19", 1770.50415, 38553, 0, 0, 0, 0, 0, 0, 5001.303585, 551733, 6275.43, 2531400,
)
您可以通过为每个重复行创建唯一的 ID 号来解决此问题:
You can fix the issue by creating a unique id number for each duplicated row:
d1 %>%
pivot_longer(cols = -Date) %>%
separate(name, into=c('partner', 'parameter'), sep='_') %>%
group_by(Date, partner, parameter) %>%
mutate(row_num = 1:n()) %>%
ungroup() %>%
pivot_wider(names_from = parameter, values_from = value) %>%
dplyr::group_by(partner) %>%
dplyr::summarise( Total_Count = sum(as.numeric(count)),
Total_Sale = sum(as.numeric(sale)))
这篇关于pivot_longer 和 pivot_wider 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!