0

我有一个看起来像这样的数据框:

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

我希望它看起来像这样:

coef   alpha  beta1  beta2
value   22    11      33

什么是 reshape/cast/dcast 逻辑?

谢谢你

4

1 回答 1

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.

于 2013-02-11T20:39:49.373 回答