0

I'm following up on this question. I was wondering why my pivot_wider() call below returns nested output for its 5 integer values with a warning?

library(tidyverse)
m="
    intrcpt name 
 1  0.0303  AL   
 2  0.0511  PD   
 3 -0.269   AL   
 4 -0.126   PD   
 5  0.195   AL   
 6  0.0647  PD   
 7  0.00690 AL   
 8 -0.0471  PD   
 9  0.0375  AL   
10  0.0571  PD
"
dat <- read.table(text = m,h=T)

pivot_wider(dat, 1, values_from = intrcpt, names_from = name)

# A tibble: 1 x 2
#  AL        PD       
#  <list>    <list>   
#1 <dbl [5]> <dbl [5]>
#Warning message:
#Values are not uniquely identified; output will contain list-cols.
#* Use `values_fn = list` to suppress this warning.
#* Use `values_fn = length` to identify where the duplicates arise
#* Use `values_fn = {summary_fun}` to summarise duplicates 
Reza
  • 299
  • 1
  • 6

1 Answers1

1

The answer depends on what you were expecting to happen.

If you expected output to have 5 rows, one for each value then you need a unique id column which "tells" which of the 5 value should go in 1st row, which one in 2nd row and so on. Right now, you don't have any column that differentiates them, hence everything is collapsed into one cell as a list. That is what the warning is telling you by Values are not uniquely identified; output will contain list-cols.. If you need output as 5 rows then you can create a new column that uniquely identify each value by using

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


If you expected to have only one value for each column then you need to "tell" how to summarise those 5 values into one. For example, you want to sum those 5 values then you can use -

tidyr::pivot_wider(dat, 1, values_from = intrcpt, names_from = name, values_fn = sum)

#         AL          PD
#       <dbl>       <dbl>
#1 0.00070000 -0.00020000

You can use any summarising function instead of sum.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213