简体   繁体   English

如何更改reshape2包中的melt.data.frame函数将“变量”列返回到“字符”类?

[英]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. melt.data.frame的默认行为是返回“factor”类中的“variable”列。 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. 我尝试了options(stringsAsFactors = FALSE)但它不起作用。

I don't believe there is such an option built into melt.data.frame . 我不相信在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 : 我们可以定义一个新函数melt.df ,用快速检查替换相关的行,看看用户是否设置了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. 我不相信这种修改在其他情况下不会产生令人惊讶的行为。

With most users heading over to either the "tidyverse" or "data.table" for reshaping data these days, your options have improved. 由于大多数用户现在都要使用“tidyverse”或“data.table”来重塑数据,所以您的选择已经有所改进。

In the "tidyverse", the default behavior is to keep the molten variable as character s: 在“tidyverse”中,默认行为是将熔化变量保持为character s:

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 . 在“data.table”实施的melt ,一些新的论点已被添加,其中之一是variable.factor可以设置为FALSE It's set to TRUE by default for, I believe, consistency with the "reshape2" implementation of melt . 默认情况下,它设置为TRUE ,我相信,与“reshape2”实现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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM