reshape2和melt警告信息

65

我正在使用 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个回答

105

解释:

当你使用melt函数时,你是将多个列合并成一个。在这种情况下,你正在合并因子列,每个因子列都有一个levels属性。由于你的因子实际上是不同的,所以这些级别在列之间是不相同的。在创建结果中的value列时,melt只会将每个因子强制转换为字符并删除它们的属性。

在这种情况下,警告并不重要,但是当合并不同“类型”的列时,你需要非常小心,“类型”不仅指向量类型,而且通常指它所涉及的事物的本质。例如,我不想将包含MPH速度的列与包含LBs重量的列融合。

确认可以合并您的因子列的一种方法是问自己是否在另一列中的任何可能值都是每个其他列中具有合理值的可能值。如果是这种情况,则正确的做法可能是确保每个因子列都具有其可能接受的所有可能级别(按相同顺序)。如果这样做,你将不会在融合表时收到警告。

说明:

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

这里我们 融化 并查看列 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的原因。如果您没有使用该设置,则会得到一个字符向量,但没有警告。我认为默认行为应该保持结果作为因子,但在这里不是这种情况。

注意:data.framestringsAsFactors 的默认值曾经是 TRUE,但现在变成了 FALSE。如果你使用上面的示例代码,你可能需要将其中一行改为 DF <- data.frame(id=1:3, x=letters[1:3], y=rev(letters)[1:3], stringsAsFactors = TRUE) - steveb

2

BrodieG的回答很好,但是有些情况下,重构列是不切实际的(例如我想将拥有128个固定宽度列的GHCN气候数据融合成更少的列)。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接