混淆基础重塑的例子

时间:2017-06-21 09:38:33

标签: r reshape

请解释一下这种行为:

为什么宽和宽2不相同,为什么重塑在广泛但不在宽2上工作?

wide <- reshape(Indometh, v.names = "conc", idvar = "Subject",
                timevar = "time", direction = "wide")
wide
#    Subject conc.0.25 conc.0.5 conc.0.75 conc.1 conc.1.25 conc.2 conc.3 conc.4 conc.5 conc.6 conc.8
# 1        1      1.50     0.94      0.78   0.48      0.37   0.19   0.12   0.11   0.08   0.07   0.05
# 12       2      2.03     1.63      0.71   0.70      0.64   0.36   0.32   0.20   0.25   0.12   0.08
# 23       3      2.72     1.49      1.16   0.80      0.80   0.39   0.22   0.12   0.11   0.08   0.08
# 34       4      1.85     1.39      1.02   0.89      0.59   0.40   0.16   0.11   0.10   0.07   0.07
# 45       5      2.05     1.04      0.81   0.39      0.30   0.23   0.13   0.11   0.08   0.10   0.06
# 56       6      2.31     1.44      1.03   0.84      0.64   0.42   0.24   0.17   0.13   0.10   0.09
reshape(wide) # ok
wide2 <- wide[,1:ncol(wide)]
reshape(wide2) # Error in match.arg(direction, c("wide", "long")) : argument "direction" is missing, with no default

一些诊断:

identical(wide,wide2)        # FALSE
dplyr::all_equal(wide,wide2) # TRUE
all.equal(wide,wide2) 
# [1] "Attributes: < Names: 1 string mismatch >"                                                       "Attributes: < Length mismatch: comparison on first 2 components >"                             
# [3] "Attributes: < Component 2: Modes: list, numeric >"                                              "Attributes: < Component 2: names for target but not for current >"                             
# [5] "Attributes: < Component 2: Length mismatch: comparison on first 5 components >"                 "Attributes: < Component 2: Component 1: Modes: character, numeric >"                           
# [7] "Attributes: < Component 2: Component 1: target is character, current is numeric >"              "Attributes: < Component 2: Component 2: Modes: character, numeric >"                           
# [9] "Attributes: < Component 2: Component 2: target is character, current is numeric >"              "Attributes: < Component 2: Component 3: Modes: character, numeric >"                           
# [11] "Attributes: < Component 2: Component 3: target is character, current is numeric >"              "Attributes: < Component 2: Component 4: Numeric: lengths (11, 1) differ >"                     
# [13] "Attributes: < Component 2: Component 5: Modes: character, numeric >"                            "Attributes: < Component 2: Component 5: Lengths: 11, 1 >"                                      
# [15] "Attributes: < Component 2: Component 5: Attributes: < Modes: list, NULL > >"                    "Attributes: < Component 2: Component 5: Attributes: < Lengths: 1, 0 > >"                       
# [17] "Attributes: < Component 2: Component 5: Attributes: < names for target but not for current > >" "Attributes: < Component 2: Component 5: Attributes: < current is not list-like > >"            
# [19] "Attributes: < Component 2: Component 5: target is matrix, current is numeric >"

1 个答案:

答案 0 :(得分:2)

因为wide data.frame上的子集操作会删除由reshape添加的自定义属性,并由reshape本身用于自动执行相反的重新整形。

事实上,您可以注意到wide包含reshapeWide的属性列表,其中包含了还原重塑的所有必要信息:

> names(attributes(wide))
[1] "row.names"   "names"       "class"       "reshapeWide"

> attributes(wide)$reshapeWide
$v.names
[1] "conc"

$timevar
[1] "time"

$idvar
[1] "Subject"

$times
 [1] 0.25 0.50 0.75 1.00 1.25 2.00 3.00 4.00 5.00 6.00 8.00

$varying
     [,1]        [,2]       [,3]        [,4]     [,5]        [,6]     [,7]     [,8]     [,9]     [,10]    [,11]   
[1,] "conc.0.25" "conc.0.5" "conc.0.75" "conc.1" "conc.1.25" "conc.2" "conc.3" "conc.4" "conc.5" "conc.6" "conc.8"

虽然wide2没有:

> names(attributes(wide2))
[1] "names"     "class"     "row.names"