簡體   English   中英

reshape2融化警告信息

[英]reshape2 melt warning message

我正在使用melt並遇到以下警告消息:
attributes are not identical across measure variables; they will be dropped

環顧四周后人們提到它是因為變量是不同的類; 但是,我的數據集不是這種情況。

這是數據集:

test <- structure(list(park = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("miss", "piro", "sacn", "slbe"), class = "factor"), 
    a1.one = structure(c(3L, 1L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 
    3L), .Label = c("agriculture", "beaver", "development", "flooding", 
    "forest_pathogen", "harvest_00_20", "harvest_30_60", "harvest_70_90", 
    "none"), class = "factor"), a2.one = structure(c(6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("development", 
    "forest_pathogen", "harvest_00_20", "harvest_30_60", "harvest_70_90", 
    "none"), class = "factor"), a3.one = structure(c(3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("forest_pathogen", 
    "harvest_00_20", "none"), class = "factor"), a1.two = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("agriculture", 
    "beaver", "development", "flooding", "forest_pathogen", "harvest_00_20", 
    "harvest_30_60", "harvest_70_90", "none"), class = "factor"), 
    a2.two = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L), .Label = c("development", "forest_pathogen", "harvest_00_20", 
    "harvest_30_60", "harvest_70_90", "none"), class = "factor"), 
    a3.two = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L), .Label = c("forest_pathogen", "harvest_00_20", "none"
    ), class = "factor")), .Names = c("park", "a1.one", "a2.one", 
"a3.one", "a1.two", "a2.two", "a3.two"), row.names = c(NA, 10L
), class = "data.frame")

這是結構:

str(test)
'data.frame':   10 obs. of  7 variables:
 $ park  : Factor w/ 4 levels "miss","piro",..: 1 1 1 1 1 1 1 1 1 1
 $ a1.one: Factor w/ 9 levels "agriculture",..: 3 1 3 3 3 3 1 3 3 3
 $ a2.one: Factor w/ 6 levels "development",..: 6 6 6 6 6 6 6 6 6 6
 $ a3.one: Factor w/ 3 levels "forest_pathogen",..: 3 3 3 3 3 3 3 3 3 3
 $ a1.two: Factor w/ 9 levels "agriculture",..: 3 3 3 3 3 3 3 3 3 3
 $ a2.two: Factor w/ 6 levels "development",..: 6 6 6 6 6 6 6 6 6 6
 $ a3.two: Factor w/ 3 levels "forest_pathogen",..: 3 3 3 3 3 3 3 3 3 3

是因為每個變量的級別數不同嗎? 那么,在這種情況下,我可以忽略警告信息嗎?

要生成警告消息:

library(reshape2)
test.m <- melt (test,id.vars=c('park'))
Warning message:
attributes are not identical across measure variables; they will be dropped

謝謝。

一個解釋:

當您融化時,您將多個列合並為一個。 在這種情況下,您將組合因子列,每個列都具有levels屬性。 這些級別在各列之間並不相同,因為您的因素實際上是不同的。 在命令結果中創建value列時, melt只是將每個因子強制轉換為字符並刪除其屬性。

在這種情況下,警告無關緊要,但是在組合不同“類型”的列時需要非常小心,其中“類型”並不僅僅意味着矢量類型,而是一般來說它指的是事物的本質。 例如,我不想在MPH中熔化包含速度的列,其中一個包含LB中的重量。

確認可以合並因子列的一種方法是問自己一列中的任何可能值是否是每個其他列中的合理值。 如果是這種情況,那么可能正確的做法是確保每個因子列都具有它可以接受的所有可能級別(以相同的順序)。 如果這樣做,當您融化桌子時,您將不會收到警告。

舉例說明:

library(reshape2)
DF <- data.frame(id=1:3, x=letters[1:3], y=rev(letters)[1:3])
str(DF)

xy的級別不一樣:

'data.frame':  3 obs. of  3 variables:
$ id: int  1 2 3
$ x : Factor w/ 3 levels "a","b","c": 1 2 3
$ y : Factor w/ 3 levels "x","y","z": 3 2 1

在這里,我們melt並查看列xy meltvalue ):

melt(DF, id.vars="id")$value

我們得到一個字符向量和一個警告:

[1] "a" "b" "c" "z" "y" "x"
Warning message:
attributes are not identical across measure variables; they will be dropped 

但是,如果我們將因子重置為具有相同的水平,然后才會融化:

DF[2:3] <- lapply(DF[2:3], factor, levels=letters)
melt(DF, id.vars="id", factorsAsStrings=F)$value

我們得到正確的因素而沒有警告:

[1] a b c z y x
Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

melt的默認行為是即使它們相同也會降低因子水平,這就是我們使用上述factorsAsStrings=F原因。 如果您沒有使用該設置,您將獲得一個字符向量,但沒有警告。 我認為默認行為應該是將結果保持為一個因素,但這不是這種情況。

BrodieG的答案很棒; 然而,在某些情況下,重構色譜柱是不切實際的(例如GHCN氣候數據,我想將128個固定寬度的色譜柱熔化成更少數量的色譜柱)。

在這種情況下,最簡單的解決方案是將數據視為字符而不是因素:例如,您可以使用read.fwf(filename,stringsAsFactors=FALSE)重新導入數據(同樣的想法適用於read.csv ) 。 對於較少數量的列,您可以使用d$mystring<-as.character(d$myfactor)將因子轉換為字符串。

暫無
暫無

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

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