首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >reshape2熔化警告消息

reshape2熔化警告消息
EN

Stack Overflow用户
提问于 2014-09-05 23:06:37
回答 2查看 46.2K关注 0票数 62

我正在使用melt,并遇到以下警告消息:

attributes are not identical across measure variables; they will be dropped

在环顾四周后,人们提到这是因为变量是不同的类;然而,我的数据集不是这样的。

以下是数据集:

代码语言:javascript
复制
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")

结构是这样的:

代码语言:javascript
复制
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

是因为每个变量的层数不同吗?那么,在这种情况下,我可以忽略警告消息吗?

要生成警告消息,请执行以下操作:

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

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-05 23:26:51

一个解释:

当你融化的时候,你是在将多个列合并成一个列。在本例中,您将组合因子列,每个因子列都有一个levels属性。这些水平在不同的列中并不相同,因为您的系数实际上是不同的。在结果中创建value列时,melt只是将每个因子强制转换为字符,并删除它们的属性。

在这种情况下,警告无关紧要,但在组合不属于相同“类型”的列时需要非常小心,其中“类型”不仅仅是指向量类型,而是泛指它所引用的事物的性质。例如,我不希望将包含速度的柱子与包含重量的柱子熔化。

确认组合因子列的一种方法是问问自己,一列中的任何可能值是否都是其他列中的合理值。如果是这样,那么正确的做法可能是确保每个因子列都具有它可以接受的所有可能级别(以相同的顺序)。如果执行此操作,则在融化表格时不会收到警告。

下面是一个示例:

代码语言:javascript
复制
library(reshape2)
DF <- data.frame(id=1:3, x=letters[1:3], y=rev(letters)[1:3])
str(DF)

xy的级别不同:

代码语言:javascript
复制
'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):

代码语言:javascript
复制
melt(DF, id.vars="id")$value

我们得到一个字符向量和一个警告:

代码语言:javascript
复制
[1] "a" "b" "c" "z" "y" "x"
Warning message:
attributes are not identical across measure variables; they will be dropped 

但是,如果我们将因子重置为相同的级别,然后才会融化:

代码语言:javascript
复制
DF[2:3] <- lapply(DF[2:3], factor, levels=letters)
melt(DF, id.vars="id", factorsAsStrings=F)$value

我们得到了正确的因子,并且没有警告:

代码语言:javascript
复制
[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的原因。如果您没有使用该设置,您将获得一个字符向量,但不会出现任何警告。我认为默认行为应该是将结果作为一个因素保留,但这里不是这样的。

票数 99
EN

Stack Overflow用户

发布于 2017-05-06 14:33:04

BrodieG的答案很好;但是,在某些情况下,重构列是不切实际的(例如,我希望将具有128个固定宽度列的GHCN气候数据融合到更少的列数中)。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25688897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档