We can use dcast
from library(data.table)
which can take multiple value.var
columns.
We convert the 'data.frame' to 'data.table' (setDT(df1)
), grouped by 'name', we create a sequence column ('N'), then use dcast
and specify the value.var
columns.
library(data.table)
setDT(df1)[, N:= 1:.N, name]
dcast(df1, name~N, value.var=c("status", "text"))
# name status_1 status_2 text_1 text_2
#1: stock1 open closed text1 text something here
Or a base R
option is reshape
after creating a sequence column by "name".
df2 <- transform(df1, N= ave(seq_along(name), name, FUN=seq_along))
reshape(df2, idvar="name", timevar="N",direction="wide")
# name status.1 text.1 status.2 text.2
#1 stock1 open text1 closed text something here
data
df1 <- structure(list(name = c("stock1", "stock1"),
status = c("open",
"closed"), text = c("text1 ", "text something here")),
.Names = c("name",
"status", "text"), class = "data.frame",
row.names = c(NA, -2L))