0

我对重塑功能有疑问。我已经厌倦了一段时间,但它只是不起作用。我的数据如下所示:

    KeyItem    Year    Value
    Income     2011     10000
    Income     2010     29299
    Depth      2010     29393
    Market Cap 2010     39393
    Depth      2011     20000
    Market Cap 2011     30000

对于我的功能,我需要它看起来像这样:

   KeyItem        2011         2010
   Income         10000        29299
   Depth          20000        29393
   Market Cap     30000        39393
4

3 回答 3

4

最简单的方法是使用包dcast中的函数reshape2。首先,加载数据:

dd = read.table(textConnection("KeyItem Year Value
Income 2011 10000
Income 2010 29299
Depth 2010 29393
Market 2010 39393
Depth 2011 20000
Market 2011 30000"), header=TRUE)

然后加载包:

library(reshape2)

最后,只是dcast函数:

dcast(dd, KeyItem ~ Year)

要得到:

R> dcast(dd, KeyItem ~ Year)
Using Value as value column: use value.var to override.
  KeyItem  2010  2011
1   Depth 29393 20000
2  Income 29299 10000
3  Market 39393 30000

反之,只需使用以下melt功能:

melt(dcast(dd, KeyItem ~ Year))

您可以按照通常的方式重新排列列:

dd1 = dcast(dd, KeyItem ~ Year) 
dd1[,c("KeyItem", sort(colnames(dd1[, 2:ncol(dd1)]),TRUE))]
于 2012-11-06T08:55:11.010 回答
3
df <- read.table(text="KeyItem    Year    Value
Income     2011     10000
Income     2010     29299
Depth      2010     29393
Market_Cap 2010     39393
Depth      2011     20000
Market_Cap 2011     30000",header=TRUE)

library(reshape2)
df2 <- dcast(df,KeyItem~Year)

#     KeyItem  2010  2011
#1      Depth 29393 20000
#2     Income 29299 10000
#3 Market_Cap 39393 30000
于 2012-11-06T08:55:14.283 回答
0

“reshape” 和 “reshape2” 包通常会得到大家的喜爱,但在 base R 中也有选项。假设你data.frame被称为“df”,这里有两个(半)基本 R 选项:

## Using base R reshape()
reshape(df, direction = "wide", idvar="KeyItem", timevar="Year")
#      KeyItem Value.2011 Value.2010
# 1     Income      10000      29299
# 3      Depth      20000      29393
# 4 Market_Cap      30000      39393

## Using xtabs()
xtabs(Value ~ KeyItem + Year, df)
#             Year
# KeyItem       2010  2011
#   Depth      29393 20000
#   Income     29299 10000
#   Market_Cap 39393 30000

## Here's the "half": Using xtabs(), but as a data.frame
as.data.frame.matrix(xtabs(Value ~ KeyItem + Year, df))
#             2010  2011
# Depth      29393 20000
# Income     29299 10000
# Market_Cap 39393 30000
于 2012-12-27T18:04:07.380 回答