【问题标题】:reshape::melt unexpected behavior with characters in data.framereshape::melt data.frame 中字符的意外行为
【发布时间】:2012-12-12 19:45:09
【问题描述】:

我回到了我上个月编写的一些 R 代码,但我使用的 reshape 版本(0.8.4,R 2.15.2)似乎改变了该功能。

这是一个示例:

> library(reshape)
> so.test <- data.frame(
                       one = as.character(rnorm(750)),
                       two = as.character(rnorm(750)),
                       three = as.character(rnorm(750)), four = as.character(rnorm(750)))
> check <- melt(so.test)

Using one, two, three, four as id variables

这给出了一个与原始数据相同的data.frame:

> table(so.test == check)

TRUE 
3000 

我也用 reshape2::melt 试过这个,但我得到了相同的结果。有趣的是,melt() 函数可以与带有数值的 data.frame 一起正常工作:

> so.test2 <- data.frame(
                      one = rnorm(750),
                      two = rnorm(750),
                      three = rnorm(750), four = rnorm(750))
> check2 <- melt(so.test2)
Using  as id variables
> head(check2)
  variable      value
1      one  0.2471168
2      one -0.0663480
3      one -0.0251867
4      one  2.8786207
5      one -0.2586785
6      one -0.7508927

【问题讨论】:

    标签: r dataframe reshape


    【解决方案1】:

    reshape 和 reshape2 版本的文档都说:

    如果您都不提供,melt 将假定因子和字符变量是 id 变量,而所有其他变量都是测量的。

    所以melt 的行为符合记录。

    【讨论】:

    • 你是对的,一定是我代码上游的一个错误。它正在成为那些日子之一......
    猜你喜欢
    • 1970-01-01
    • 2015-10-28
    • 2021-12-08
    • 2020-10-09
    • 2014-08-07
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 2021-06-13
    相关资源
    最近更新 更多