. Advertisement .
..3..
. Advertisement .
..4..
I am working on r, but I found the following warning message:
library(reshape2)
test.m <- melt (test,id.vars=c('park'))
Warning message:
attributes are not identical across measure variables; they will be dropped
Is there any way to stabilize the issue “warning message: attributes are not identical across measure variables; they will be dropped”? I read a lot of topics about this, but all of them were trying to install anything. Is this the correct way, or any recommendation for me? Please find the beginning command below:
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
*** End Block ***
The cause:
You are having a trouble with this error because you combine several columns into one when you
melt
. You are merging factor columns in this instance, and each of those columns has a levels attribute. Because your components are really changeable across the columns, these levels are not similar. When forming the value column in the result,melt
just forces each element to take on a character and discards its properties.Solution:
When merging columns that are not the same “type”, which encompasses not just vector ”type” but also the general sort of items it refers to, you must exercise extreme caution.
Asking yourself whether any potential values in one column would be a suitable value to have in each other column is a solution to determine whether it is okay to combine your factor columns or not. If that’s the case, probably you had better make sure that each factor column contains all of the levels that it could allow (in the same order). This will prevent you from receiving a notice when the table
melt
. Let’s follow the below example to understand clearly.x
andy
levels are completely different:I
melt
here and let’s examine the column which was formed whenx
andy
melted into (value
):I receive a character of vector and a warning message:
However, if I reset the factors so that their levels are similar and I only
melt
:Then I get the exact factors and there is no warning for me:
I use
factorsAsStrings=F
above becausemelt
default behavior is to reduce factor levels even when they are similar. You would have received a character vector but no warning if you hadn’t used that setting. I suppose the default action here should keep the result as a consideration.BrodieG’s excellent answer is great. However, there are cases when it is not practical to refactor columns. For example, GHCN climate data has 128 columns fixed-width that I wanted to melt down into a smaller number of columns.
The simplest way to deal with this situation is to treat data as characters, rather than factors. For example,
read.fwf(filename,stringsAsFactors=FALSE)
can be used to re-import data. This same process would apply forread.csv
. You could used$mystring<-as.character(d$myfactor)
to convert factors into strings for a smaller number column.