reshape2融化警告信息

时间:2014-09-05 15:06:37

标签: r reshape2 melt

我使用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

感谢。

2 个答案:

答案 0 :(得分:78)

解释:

当您融化时,您将多个列合并为一个。在这种情况下,您将组合因子列,每个列都具有levels属性。这些级别在各列之间并不相同,因为您的因素实际上是不同的。在结果中创建melt列时,value只是将每个因素强制转换为字符并删除其属性。

在这种情况下,警告无关紧要,但在组合不同“类型”的列时需要非常小心,其中“类型”并不仅仅意味着矢量类型,而是一般事物的本质它指的是。例如,我不希望熔化包含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已融入(value):

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的原因。如果您没有使用该设置,您将获得一个字符向量,但没有警告。我认为默认行为应该是将结果保持为一个因素,但这不是这种情况。

答案 1 :(得分:1)

BrodieG的答案非常好;然而,在某些情况下,重构色谱柱是不切实际的(例如GHCN气候数据,我想将128个固定宽度的色谱柱熔化成更少数量的色谱柱)。

在这种情况下,最简单的解决方案是将数据视为字符而不是因素:例如,您可以使用read.fwf(filename,stringsAsFactors=FALSE)重新导入数据(同样的想法适用于read.csv) 。对于较少数量的列,您可以使用d$mystring<-as.character(d$myfactor)将因子转换为字符串。