0

我正在处理一个参差不齐的数据框,该数据框在第一列中包含一列时间点,在第一行中包含序列号列表,在数据框的其余部分中包含实际库存数据(项目数量)。

> mydf
    V1             V2             V3             V4             V5
1 month item_serial123 item_serial234 item_serial345 item_serial456
2     0            234            120            302            500
3     1            344            125            350            450
4     2            235            129            400            300
5     3            453            145            450            330
6     4            200            130            500            200
7     5            201                           501               
8     6                                          504            202

我正在尝试格式化数据,以便我有一个“长”列表,以便我可以对每个项目的序列号进行分析。我可以从列表中丢弃非数字数据,并通过在 中设置stringsAsFactors=FALSE标志来确保将数据作为字符对象导入read.table,然后将 mydf 转换为数据矩阵:

> mydf.new<-data.matrix(mydf)
Warning in data.matrix(mydf) : NAs introduced by coercion
Warning in data.matrix(mydf) : NAs introduced by coercion
Warning in data.matrix(mydf) : NAs introduced by coercion
Warning in data.matrix(mydf) : NAs introduced by coercion
Warning in data.matrix(mydf) : NAs introduced by coercion
> mydf.new
     V1  V2  V3  V4  V5
[1,] NA  NA  NA  NA  NA
[2,]  0 234 120 302 500
[3,]  1 344 125 350 450
[4,]  2 235 129 400 300
[5,]  3 453 145 450 330
[6,]  4 200 130 500 200
[7,]  5 201  NA 501  NA
[8,]  6  NA  NA 504 202

将变量 V1 更改为“时间”是微不足道的。我真正苦苦挣扎的是如何mydf[1,2:5]在我融化/铸造时从中提取序列号并将它们分配给适当的数据mydf.new。我想结束的是这样的:

   time count serial_number
   0    234 item_serial123
   1    344 item_serial123
   2    235 item_serial123
   3    453 item_serial123
   4    200 item_serial123
   5    201 item_serial123
   6    NA  item_serial123

等等等等。有什么建议吗?

4

1 回答 1

0

If I correctly understood your question, then you have a data.frame like this:

> df
  month item_serial123 item_serial234 item_serial345 item_serial456
1     0            234            120            302            500
2     1            344            125            350            450
3     2            235            129            400            300
4     3            453            145            450            330
5     4            200            130            500            200
6     5            201             NA            501             NA
7     6             NA             NA            504            202 

now, you can use reshape to get the following:

> df_new <- reshape(df, idvar = "month",  varying = list(2:5), 
                    v.names="item_serial", direction = "long",
                    new.row.names=1:(prod(dim(df[,-1]))))
> df_new$time <- factor(df_new$time, labels=names(df)[-1])
> df_new
   month           time item_serial  # you may want to use `colnames`to chance them
1      0 item_serial123         234
2      1 item_serial123         344
3      2 item_serial123         235
4      3 item_serial123         453
5      4 item_serial123         200
6      5 item_serial123         201
7      6 item_serial123          NA
8      0 item_serial234         120
于 2013-11-06T23:54:48.163 回答