pivot_wider issue "Values in `values_from` are not uniquely identified; output will contain list-cols" pivot_wider issue "Values in `values_from` are not uniquely identified; output will contain list-cols" r r

pivot_wider issue "Values in `values_from` are not uniquely identified; output will contain list-cols"


Create a unique identifier row for each name and then use pivot_wider

library(dplyr)d %>%  group_by(name) %>%  mutate(row = row_number()) %>%  tidyr::pivot_wider(names_from = name, values_from = val) %>%  select(-row)# A tibble: 51 x 4#   time          x1 `C Farolillo` `Plaza Eliptica`#   <date>     <dbl>         <dbl>            <dbl># 1 2016-04-20  51.5             7               32# 2 2016-04-21  56.3             3               25# 3 2016-04-22  56.3             7               31# 4 2016-04-23  57.9            13               34# 5 2016-04-24  58.7             7               26# 6 2016-04-25  59.0             9               33# 7 2016-04-26  64.5            20               35# 8 2016-04-27  61.9            19               43# 9 2016-04-28  60.3             4               22#10 2016-04-29  59.4             5               22# … with 41 more rows


Typically the error

Warning message:Values in `val` are not uniquely identified; output will contain list-cols.

is most often caused by duplicate rows in the data (after excluding the val column), and not duplicates in the val column.

which(duplicated(d))# [1] 14 65

OP's data seems to have two duplicate rows which is causing this issue. Removing the duplicate rows also gets rid of the error.

yy <- d %>% distinct() %>% pivot_wider(., names_from = name, values_from = val)yy
# A tibble: 50 x 4   time          x1 `C Farolillo` `Plaza Eliptica`   <date>     <dbl>         <dbl>            <dbl> 1 2016-04-20  51.5             7               32 2 2016-04-21  56.3             3               25 3 2016-04-22  56.3             7               31 4 2016-04-23  57.9            13               34 5 2016-04-24  58.7             7               26 6 2016-04-25  59.0             9               33 7 2016-04-26  64.5            20               35 8 2016-04-27  61.9            19               43 9 2016-04-28  60.3             4               2210 2016-04-29  59.4             5               22# ... with 40 more rows


The problem is caused by the fact that the data that you want to spread / pivot wider has duplicate identifiers. While both suggestions above, i.e. creating a unique artifical id from row numbers with mutate(row = row_number()) , or filtering only distinct rows will allow you to pivot wider, but they change the structure of your table, which is likely to have an logical, organizational problem that will come out next time you try to join anything to it.

It is a much better practice to use the id_cols parameter explicity, to see that you actually want to have to be unique after pivotting wide, and if you are running into problems, re-organize the original table first. Of course, you may find reason for filtering to distinct rows, or adding a new ID, most likely you will want to avoid the duplication earlier in your code.