我越来越熟悉R并学习长宽数据帧。我正在使用dcast(和ddply),但据我所知,他们依赖于我的数据。在以下示例中,我有:
data.frame(color=c("red","orange","blue","white"),safe=c("N","N","Y","Y"))
基本上,旧的假设是保险公司惩罚了风险的颜色"汽车不太安全。我想要一个把它变成一张宽桌子的命令。是否缺少dcast的风格或语法,将上表变为
red | orange | blue | white
N | N | Y | Y
感谢您的帮助。
答案 0 :(得分:0)
也许是transpose
功能?
a <- data.frame(color=c("red","orange","blue","white"),safe=c("N","N","Y","Y"))
# transpose and make it a dataframe.
new_a <- data.frame(t(a), stringsAsFactors=FALSE)
# makes the column names the first row of the new dataframe
names(new_a) <- new_a[1,]
# now you can get rid of the first row.
new_a <- new_a[-1,]
> new_a
red orange blue white
safe N N Y Y