1

How do I merge rows by one columns value? Suppose, we've got df:

variable  value
x1        a
x2        a
x1        b
x2        b

And I need data frame like this:

variable  value1  value2
x1        a       b
x2        a       b
Jaap
  • 81,064
  • 34
  • 182
  • 193
Katerina
  • 2,580
  • 1
  • 22
  • 25
  • What have you tried so far? Can you figure out why it didn't work? Please edit the question to add more information (i.e., code) that helps us better understand where you need help. – Andrew Gies Dec 09 '15 at 07:36
  • `reshape` is the searched topic – jogo Dec 09 '15 at 07:40

1 Answers1

0

We can try

library(data.table)
setDT(dfN)[, ind:= paste0("Value", 1:.N), variable]
dcast(dfN, variable~ind, value.var='value')
#  variable Value1 Value2
#1:       x1      a      b
#2:       x2      a      b

Or using dplyr

library(dplyr)
library(tidyr)
dfN %>%
    group_by(variable) %>%
    mutate(ind= row_number()) %>%
    spread(ind, value)
akrun
  • 874,273
  • 37
  • 540
  • 662