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
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
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)