Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 重新格式化警告消息_R_Reshape2_Melt - Fatal编程技术网

R 重新格式化警告消息

R 重新格式化警告消息,r,reshape2,melt,R,Reshape2,Melt,我正在使用melt时遇到以下警告消息: 属性在度量变量之间不相同;它们将被删除 环顾四周,人们都提到这是因为变量是不同的类;但是,我的数据集并非如此 以下是数据集: 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 = str

我正在使用
melt
时遇到以下警告消息:
属性在度量变量之间不相同;它们将被删除

环顾四周,人们都提到这是因为变量是不同的类;但是,我的数据集并非如此

以下是数据集:

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")
是因为每个变量的级别数不同吗?那么,在这种情况下,我可以忽略警告信息吗

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

library(reshape2)
test.m <- melt (test,id.vars=c('park'))
Warning message:
attributes are not identical across measure variables; they will be dropped
library(重塑2)
我是一个解释:
熔化时,将多个列合并为一个列。在本例中,您将合并因子列,每个列都有一个
levels
属性。这些级别在各列中是不同的,因为您的因子实际上是不同的
melt
在结果中创建
列时,仅强制每个因子为字符并删除其属性

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

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

举例说明: 在这里,我们将
melt
,并查看
x
y
列被熔入(
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

melt
的默认行为是降低因子水平,即使它们是相同的,这就是我们在上面使用
factorsAsStrings=F
的原因。如果您没有使用该设置,您将得到一个字符向量,但没有警告。我认为默认行为应该是将结果作为一个因素,但这里不是这样;但是,在某些情况下,重构列是不切实际的(例如,GHCN气候数据具有128个固定宽度的列,我希望将这些列融合到数量少得多的列中)

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