2

With the dataset df:

 df
 confint        row Index
 0.3407,0.4104    1     1
 0.2849,0.4413    2     2
 0.2137,0.2674    3     3
 0.1910,0.4575    4     1
 0.4039,0.4905    5     2
 0.403,0.4822     6     3
 0.0301,0.0646    7     1
 0.0377,0.0747    8     2
 0.0835,0.0918    9     3
 0.0437,0.0829   10     1
 0.0417,0.0711   11     2
 0.0718,0.0798   12     3
 0.0112,0.0417   13     1
 0.019,0.0237    14     2
 0.0213,0.0293   15     3
 0.0121,0.0393   16     1
 0.0126,0.0246   17     2
 0.0318,0.0428   18     3
 0.0298,0.0631   19     1
 0.018,0.0202    20     2
 0.1031,0.1207   21     3

This should be a rather easy dataset to convert from long to wide form that is a 7 (row) x 3 (column) dataframe. The result should have 3 columns named by Index and 7 rows (21/3 = 7). The code is as of the following:

df <- spread(df,Index, confint, convert = FALSE)

However, by using Spread() I received the following error:

Error: Duplicate identifiers for rows (1, 4, 7, 10, 13, 16, 19), (2, 5, 8, 11, 14, 17, 20), (3, 6, 9, 12, 15, 18, 21)

Any help will be greatly appreciated!

lydias
  • 713
  • 7
  • 24
  • Not able to reproduce. `spread(df, Index, confint)` works for me. Also your attempt works fine `spread(df,Index, confint, convert = FALSE)` – Ronak Shah Jan 07 '19 at 03:18

1 Answers1

3

We need to create a sequence column and then spread

library(tidyverse)
df %>%
  group_by(Index) %>%
  mutate(ind = row_number()) %>%
  spread(Index, confint, convert = FALSE)

NOTE: This would be an issue in the original dataset and not in the example data showed in the post

akrun
  • 674,427
  • 24
  • 381
  • 486