【问题标题】:pivot_wider causing warning Values in `temp` are not uniquely identified; output will contain list-colspivot_wider 导致警告 `temp` 中的值不是唯一标识的;输出将包含 list-cols
【发布时间】:2020-03-12 03:31:45
【问题描述】:

如果不共享私人数据,我无法提供可重现的示例。由于我不知道为什么会发生这种情况,我也无法模仿数据来重现。

我对数据框进行了 dplyr 转换:

combined_grp <- combined %>% 

  mutate(temp = 1) %>% 
  pivot_wider(names_from = TTI_Bin_John, 
              names_prefix = "TTI_",
              values_from = temp,
              values_fn = as.numeric,
              values_fill = list(temp = 0))

这会返回一个警告:

Warning message:
Values in `temp` are not uniquely identified; output will contain list-cols.
* Use `values_fn = list(temp = list)` to suppress this warning.
* Use `values_fn = list(temp = length)` to identify where the duplicates arise
* Use `values_fn = list(temp = summary_fun)` to summarise duplicates

我尝试通过在上面的块末尾添加这个来改变输出列:

%>% mutate_at(vars(matches("TTI_(<|>)")), as.numeric)

但这会引发错误:

values_fn[[value]] 中的错误:'builtin' 类型的对象不是子集

有没有人认识到这个错误或者我可以如何解决它,以便更广泛地旋转的新功能是数字而不是列表?

【问题讨论】:

  • 您在输出 x &lt;- tribble(~name, ~value, "a", 1, "b", 2, "a", 3); pivot_wider(x) 中的某些位置有重复值。如果您可以匿名数据并将其包含在内,那将很有用
  • 您可以多次向数据添加随机数,使其完全不同
  • 使用了mutate(id = row_num),这似乎解决了我的问题。但我不明白为什么我必须这样做

标签: r dplyr tidyr


【解决方案1】:

错误信息告诉你需要修改values_fn这一行。

Values in `temp` are not uniquely identified; output will contain list-cols.
* Use `values_fn = list(temp = list)` to suppress this warning.
* Use `values_fn = list(temp = length)` to identify where the duplicates arise
* Use `values_fn = list(temp = summary_fun)` to summarise duplicates

如果您将 as.numeric 函数包装在一个列表中会发生什么:

values_fn = list(temp = as.numeric)

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2020-09-28
    • 1970-01-01
    • 2014-09-28
    相关资源
    最近更新 更多