Issue with pivot_longer and pivot_wider

瘦欲@ 提交于 2019-12-11 06:06:37

问题


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>.

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/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,
)

Below is the code.

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.


回答1:


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,
)

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)))


来源:https://stackoverflow.com/questions/58331771/issue-with-pivot-longer-and-pivot-wider

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!