0

I have a data frame that looks like this:

variable=c("alpha","beta1","beta2")
value=c(22,11,33)

df=data.frame(variable=variable,
              value=value)

df

variable value
alpha    22
beta1    11
beta2    33

and I would like it to look like this:

coef   alpha  beta1  beta2
value   22    11      33

what is the reshape/cast/dcast logic?

thank you

MikeTP
  • 7,716
  • 16
  • 44
  • 57

1 Answers1

3

Using reshape2

library(reshape2)
(d <- dcast(df, 'value' ~ variable, value.var='value'))

However, to get the name of you first column right you will still need to do

names(d)[1] <- "coef"

don't know if its possible to do this in one statement.

Jan van der Laan
  • 8,005
  • 1
  • 20
  • 35