2017-06-21 70 views
0

能否請您解釋這種現象:混亂的基礎上重塑例如

爲什麼在寬WIDE2不相同,爲什麼重塑上廣泛的作品,但不是WIDE2?

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 >" 
+0

作爲顯示屬性不一樣。你可以使用'all.equal(...,check.attributes = FALSE)' – akrun

回答

1

由於上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" 
+1

非常有幫助,謝謝! –