I'm using melt and encounter the following warning message:
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")

struct 如下:

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

是因 for each 变量的级别数不同吗?那么,在这种情况下,我可以忽略警告信息吗?

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

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

谢谢

推荐答案

解释:

当你融化时,你将把多个柱组合成一个.在本例中,您将合并factor列,每个列都有一个levels属性.这些级别在不同的列中是不同的,因为您的因子实际上是不同的.melt只是将每个因子强制为character,并在结果中创建value列时删除它们的属性.

在这种情况下,警告并不重要,但在组合不属于同一"类型"的列时,需要非常小心,其中"类型"不仅仅指向量类型,而是指它所指事物的一般性质.例如,我不想将包含速度(以英里/小时为单位)的列与包含重量(以磅为单位)的列合并.

确认合并因子列是可以的一种方法是问问自己,一列中的任何可能值是否是其他列中的合理值.如果是这样的话,那么正确的做法可能是确保每个因子列都有它可以接受的所有可能的水平(以相同的顺序).如果你这样做,当你融化桌子时,你不会得到警告.

举个例子:

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

这里我们来看看柱x和柱y被熔入(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.如果你没有使用这个设置,你会得到一个字符向量,但没有警告.我认为默认行为应该是将结果作为一个因素,但这里的情况并非如此.

R相关问答推荐

创建重复删除的唯一数据集组合列表

从R中的另一个包扩展S3类的正确方法是什么

基于shiny 应用程序中的日期范围子集xts索引

在R底座中更改白天和夜晚的背景 colored颜色

用预测NLS处理R中生物学假设之上的误差传播

使用strsplit()将向量操作为数据框

使用rest从header(h2,h3,table)提取分层信息

R spatstat Minkowski Sum()返回多个边界

如何基于两个条件从一列中提取行

Data.table';S GForce-将多个函数应用于多列(带可选参数)

R+reprex:在呈现R标记文件时创建可重现的示例

扩展R中包含列表的数据框

变长向量的矢量化和

按镜像列值自定义行顺序

位置_道奇在geom_point图中不躲避

将日期列从字符转换为日期得到的结果是NAS

将每晚的平均值与每晚的值进行比较,统计是否有效?

R:水平旋转图

如何根据每个子框架中分类因子的唯一计数来过滤子框架列表?

使用点图调整离散轴比例