1

I cannot provide a reproducible example without sharing private data. Since I do not know why this happens I cannot imitate data to reproduce either.

I have a dplyr transformation on a data frame:

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

This returns a warning:

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

I tried mutating the output columns by adding this at the end of the block above:

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

But this throws an error:

Error in values_fn[[value]] : object of type 'builtin' is not subsettable

Does anyone recognize this error or how I can get around it so that the new features that were pivoted wider are numeric and not lists?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • You have duplicate values for some positions in the output `x <- tribble(~name, ~value, "a", 1, "b", 2, "a", 3); pivot_wider(x) `. If you can anonymise your data and include it, that would be useful – Richard Telford Nov 16 '19 at 00:04
  • You can just add random numbers to your data multiple times to make it totally different – Tung Nov 16 '19 at 00:05
  • 1
    used `mutate(id = row_num)` which seems to have solved my problem. But I don't understand why I had to do that – Doug Fir Nov 16 '19 at 00:10
  • Possible duplicate https://stackoverflow.com/questions/58837773/pivot-wider-issue-values-in-values-from-are-not-uniquely-identified-output-w/ – Ronak Shah Nov 16 '19 at 02:52

1 Answers1

0

The error message tells you that you need to modify the values_fn line.

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

What happens if you wrap your as.numeric function in a list as in:

values_fn = list(temp = as.numeric)
Omar Wasow
  • 1,870
  • 24
  • 24