簡體   English   中英

從數據框中的行中提取因子

[英]extracting factors from row in data frame

我正在處理一個衣衫data的數據框,該數據框在第一列中包含一列時間點,在第一行中包含序列號列表,並在其余數據框中包含實際庫存數據(項目數)。

> 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

我正在嘗試格式化數據,以便有一個“長”列表,以便可以對每個項目的序列號進行分析。 我可以從列表中丟棄非數字數據,並通過在read.table設置stringsAsFactors=FALSE標志來確保將數據作為字符對象導入,然后將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

等。有什么建議嗎?

如果我正確理解了您的問題,那么您將獲得一個像這樣的data.frame:

> 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 

現在,您可以使用reshape獲得以下內容:

> 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM