Dambo
Dambo

Reputation: 3506

How to spread columns with key and value of equal length (without creating ids)?

I am trying to spread this dataset for presentation purposes:

library(tibble)
library(tidyr)
tibble(var1 = c('a', 'a', 'a', 'b','b', 'b'), value=c(2,1,4,2,1,4)) %>% 
spread(var1, value)

Eventually I would like to have

    a     b
  <chr> <dbl>
1     2     2
2     1     1
3     4     4

But I get the error:

Error: Duplicate identifiers for rows (1, 2, 3), (4, 5, 6)

Is there a way to achieve this for datasets that would spread into columns of equal length? I am not sure why a duplicate identifier would be an issue in this particular case.

Upvotes: 0

Views: 243

Answers (1)

AntoniosK
AntoniosK

Reputation: 16121

The spread function tries to identify in which row the values should go and tries to use var1 as identifier for the rows.

As @Frank mentioned you need to create this column to be used as identifier. Identifier for rows means that the numbers you'll provide will affect the order of your values.

Check this example:

library(tidyverse)

df = tibble(var1 = c('a', 'a', 'a', 'b','b', 'b'), value=c(2,1,4,2,1,4)) 

df %>%
  group_by(var1) %>%
  mutate(id = row_number()) %>%
  spread(var1, value)

# # A tibble: 3 x 3
#     id     a     b
# * <int> <dbl> <dbl>
# 1     1     2     2
# 2     2     1     1
# 3     3     4     4

df %>%
  group_by(var1) %>%
  mutate(id = n():1) %>%
  spread(var1, value)

# # A tibble: 3 x 3
#      id     a     b
# * <int> <dbl> <dbl>
# 1     1     4     4
# 2     2     1     1
# 3     3     2     2

Upvotes: 2

Related Questions