Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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_Reshape - Fatal编程技术网

R 熔铸数据框会产生不正确的输出

R 熔铸数据框会产生不正确的输出,r,reshape,R,Reshape,我在重塑包中的铸造/熔化过程中遇到了一个奇怪的行为。如果我投射一个data.frame,然后尝试melt它,那么melt就会出错。从castdata.frame手动取消设置“df.melt”类可以使其正确熔化 有人知道这是否是预期行为吗?如果是,当您需要它时,用例是什么 一个小代码示例显示了以下行为: > df <- data.frame(type=c(1, 1, 2, 2, 3, 3), variable="n", value=c(71, 72, 68, 80, 21, 20))

我在
重塑
包中的
铸造
/
熔化
过程中遇到了一个奇怪的行为。如果我投射一个
data.frame
,然后尝试
melt
它,那么
melt
就会出错。从cast
data.frame
手动取消设置“df.melt”类可以使其正确熔化

有人知道这是否是预期行为吗?如果是,当您需要它时,用例是什么

一个小代码示例显示了以下行为:

> df <- data.frame(type=c(1, 1, 2, 2, 3, 3), variable="n", value=c(71, 72, 68, 80, 21, 20))

> df
  type variable value
1    1        n    71
2    1        n    72
3    2        n    68
4    2        n    80
5    3        n    21
6    3        n    20

> df.cast <- cast(df, type~., sum)
> names(df.cast)[2] <- "n"

> df.cast
  type   n
1    1 143
2    2 148
3    3  41

> class(df.cast)
[1] "cast_df"    "data.frame"

> melt(df.cast, id="type", measure="n")
         type value value
X.all.      1   143 (all)
X.all..1    2   148 (all)
X.all..2    3    41 (all)

> class(df.cast) <- "data.frame"
> class(df.cast)
[1] "data.frame"

> melt(df.cast, id="type", measure="n")
  type variable value
1    1        n   143
2    2        n   148
3    3        n    41
>测向
类型变量值
1 n 71
2 1 n 72
3 2 n 68
4 2 n 80
5 3 n 21
6 3 n 20
>df.cast名称(df.cast)[2]df.cast
n型
1    1 143
2    2 148
3    3  41
>等级(df.cast)
[1] “数据帧”
>熔体(df.cast,id=“type”,measure=“n”)
类型值
全部。1143(全部)
X.all..1 2 148(全部)
X.all..2 3 41(全部)
>类(df.cast)类(df.cast)
[1] “数据帧”
>熔体(df.cast,id=“type”,measure=“n”)
类型变量值
1 n 143
2 n 148
3 n 41

在投射数据帧之前,您需要熔化数据帧。不熔化的铸造会产生各种意想不到的行为,因为重塑必须猜测数据的结构。

我知道这是一个老问题,不太可能引起很多兴趣。我也不太明白为什么要做示例中演示的事情。然而,总结一下答案,要么:

  • 在再次“熔化”之前,将
    df.cast
    包装在
    as.data.frame
  • 删除“重塑”并更新为“重塑2”。当您发布此问题时,这是不适用的,因为您的问题比“重塑2”的版本1早了大约半年
  • 这是一个较长的步行路线:

    首先,我们将加载“重塑”和“重塑2”,执行“转换”,并重命名“n”变量。显然,附加了“R2”的对象是来自“重塑2”的对象,“R1”来自“重塑”

    一些观察结果是显而易见的:

    • 通过查看
      类的输出
      ,您可以猜到,如果您使用“重塑2”,您在做您想做的事情时不会遇到任何问题
    • 哇。
      str(df.cast.R1)
      的输出是我见过的最奇怪的
      data.frame
      !实际上看起来有两个单变量
      data.frame
      s
    有了这些新知识,在我们不想更改您铸造的
    data.frame
    类的前提下,让我们继续:

    # You don't want this
    melt(df.cast.R1, id="type", measure="n") 
    #          type value value
    # X.all.      1   143 (all)
    # X.all..1    2   148 (all)
    # X.all..2    3    41 (all)
    
    # You *do* want this
    melt(as.data.frame(df.cast.R1), id="type", measure="n")
    #   type variable value
    # 1    1        n   143
    # 2    2        n   148
    # 3    3        n    41
    
    # And the class has not bee altered
    class(df.cast.R1)
    # [1] "cast_df"    "data.frame"
    
    # As predicted, this works too.
    melt(df.cast.R2, id="type", measure="n")
    #   type variable value
    # 1    1        n   143
    # 2    2        n   148
    # 3    3        n    41
    

    如果您仍在从“整形”中使用<代码> Case<代码>,请考虑升级到“RESHAPE2”,或者在熔体>代码>中编写一个方便的包装器函数…也许

    melt2

    melt2 <- function(data, ...) {
      ifelse(isTRUE("cast_df" %in% class(data)), 
             data <- as.data.frame(data), 
             data <- data)
      melt(data, ...)
    }
    

    我很困惑。你为什么要融化一个已经是长格式的df?而且你使用
    cast
    也没有多大意义。通常在使用
    melt
    后再使用它。请详细解释一下您尝试执行的操作以及预期的结果。@PaulHiemstra,您没有切换的原因是什么?(问一个通常坚持基本R重塑以满足其所有重塑需求的人)我只是没有感觉到这种需求,我主要使用
    melt
    为ggplot2或plyr准备数据,仅此而已。
    # You don't want this
    melt(df.cast.R1, id="type", measure="n") 
    #          type value value
    # X.all.      1   143 (all)
    # X.all..1    2   148 (all)
    # X.all..2    3    41 (all)
    
    # You *do* want this
    melt(as.data.frame(df.cast.R1), id="type", measure="n")
    #   type variable value
    # 1    1        n   143
    # 2    2        n   148
    # 3    3        n    41
    
    # And the class has not bee altered
    class(df.cast.R1)
    # [1] "cast_df"    "data.frame"
    
    # As predicted, this works too.
    melt(df.cast.R2, id="type", measure="n")
    #   type variable value
    # 1    1        n   143
    # 2    2        n   148
    # 3    3        n    41
    
    melt2 <- function(data, ...) {
      ifelse(isTRUE("cast_df" %in% class(data)), 
             data <- as.data.frame(data), 
             data <- data)
      melt(data, ...)
    }
    
    melt2(df.cast.R, id="type", measure="n")
    #    ype variable value
    # 1    1        n   143
    # 2    2        n   148
    # 3    3        n    41