Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the melt.data.frame function in reshape2 package returned "variable" column to "character" class?

The default behavior of melt.data.frame is to return the "variable" column in "factor" class. Here is an example:

> head(airquality)

  ozone solar.r wind temp month day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6

> x = melt(head(airquality))
Using  as id variables

> head(x)
  variable value
1    ozone    41
2    ozone    36
3    ozone    12
4    ozone    18
5    ozone    NA
6    ozone    28

> class(x$variable)
[1] "factor"

The question is that is there any parameter to change the class from factor to character? I tried options(stringsAsFactors = FALSE) but it is not working.

like image 795
rninja Avatar asked Sep 14 '11 21:09

rninja


People also ask

What package is the melt function in R?

The melt function is to be found in the reshape package. If you do not have that package installed, then you will need to install it with install. packages("reshape") before you can use it. Then, when the package is installed, make it available with library(reshape) .

What does melting a DataFrame do in R?

Melting in R programming is done to organize the data. It is performed using melt() function which takes dataset and column values that has to be kept constant. Using melt(), dataframe is converted into long format and stretches the data frame.

What does melt () do?

melt() function is useful to message a DataFrame into a format where one or more columns are identifier variables, while all other columns, considered measured variables, are unpivoted to the row axis, leaving just two non-identifier columns, variable and value.

What is the opposite of melt in R?

5. 4. In package reshape2 the opposite of melt is cast.


2 Answers

I don't believe there is such an option built into melt.data.frame. However, if you inspect the code, it's not hard to change. We can define a new function melt.df that replaces the relevant line with a quick check to see if the user has set stringsAsFactors = FALSE:

if (getOption("stringsAsFactors")){
    df[[variable_name]] <- factor(df[[variable_name]], 
                                   unique(df[[variable_name]]))
}
else{
   df[[variable_name]] <- as.character(factor(df[[variable_name]],         
                                   unique(df[[variable_name]])))
}

I checked this on your simple example and it worked as expected, but I haven't checked it more generally, so beware. I'm not convinced this modification won't produce surprising behavior in other circumstances.

like image 110
joran Avatar answered Nov 08 '22 22:11

joran


With most users heading over to either the "tidyverse" or "data.table" for reshaping data these days, your options have improved.

In the "tidyverse", the default behavior is to keep the molten variable as characters:

library(tidyverse)
airquality %>% gather(var, val, everything()) %>% str()
# 'data.frame': 918 obs. of  2 variables:
#  $ var: chr  "Ozone" "Ozone" "Ozone" "Ozone" ...
#  $ val: num  41 36 12 18 NA 28 23 19 8 NA ...

In the "data.table" implementation of melt, a few new arguments have been added, one of which is variable.factor which can be set to FALSE. It's set to TRUE by default for, I believe, consistency with the "reshape2" implementation of melt.

library(data.table)
str(melt(as.data.table(airquality), variable.factor = FALSE))
# Classes ‘data.table’ and 'data.frame':    36 obs. of  2 variables:
#  $ variable: chr  "Ozone" "Ozone" "Ozone" "Ozone" ...
#  $ value   : num  41 36 12 18 NA 28 190 118 149 313 ...
#  - attr(*, ".internal.selfref")=<externalptr> 
like image 44
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 08 '22 22:11

A5C1D2H2I1M1N2O1R2T1