2

I have two columns: one that contains the name of the variables and one that contains their respective values. It is only two variables, with three observations each, like in the example that follows:

library(tidyverse)

tibble(
  var = rep(letters[1:2],3),
  vals = c(1:6)
) %>% 
  arrange(var)

# A tibble: 6 x 2
  var    vals
  <chr> <int>
1 a         1
2 a         3
3 a         5
4 b         2
5 b         4
6 b         6

The problem is that I need to turn a and b into columns, like in this expected output:

# A tibble: 3 x 2
      a     b
  <dbl> <dbl>
1     1     2
2     3     4
3     5     6

I have tried doing this with tidyr::spread with no success. Any ideas?

FMM
  • 1,857
  • 1
  • 15
  • 38
  • 1
    I'm guessing you are getting the duplicate identifiers error? You need uniquely identified rows. See comments and answer to [this question](https://stackoverflow.com/questions/48383605/spread-with-duplicate-identifiers-for-rows) – aosmith Nov 16 '18 at 20:12

2 Answers2

2

You could use unstack

tbl <- tibble(
  var = rep(letters[1:2],3),
  vals = c(1:6)
) %>% 
  arrange(var)

unstack(tbl, vals ~ var)
#  a b
#1 1 2
#2 3 4
#3 5 6
markus
  • 25,843
  • 5
  • 39
  • 58
1

We need a sequence column after grouping by 'var'. It will also make sure that for groups that are not having the same number of elements, the remaining values can be filled by a choice value (by default, it is NA)

library(tidyverse)
tbl %>% 
  group_by(var) %>% 
  mutate(rn = row_number()) %>% 
  spread(var, vals) %>% 
  select(-rn)
# A tibble: 3 x 2
#      a     b
#  <int> <int>
#1     1     2
#2     3     4
#3     5     6

data

tbl <- tibble(
  var = rep(letters[1:2],3),
  vals = c(1:6)
   ) %>% 
     arrange(var)
akrun
  • 874,273
  • 37
  • 540
  • 662