[英]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)
x
和y
的级别不一样:
'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
并查看列x
和y
melt
( 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
原因。 如果您没有使用该设置,您将获得一个字符向量,但没有警告。 我认为默认行为应该是将结果保持为一个因素,但这不是这种情况。
BrodieG的答案很棒; 然而,在某些情况下,重构色谱柱是不切实际的(例如GHCN气候数据,我想将128个固定宽度的色谱柱熔化成更少数量的色谱柱)。
在这种情况下,最简单的解决方案是将数据视为字符而不是因素:例如,您可以使用read.fwf(filename,stringsAsFactors=FALSE)
重新导入数据(同样的想法适用于read.csv
) 。 对于较少数量的列,您可以使用d$mystring<-as.character(d$myfactor)
将因子转换为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.