我有一個.csv文件是這樣的:如何重塑數據長格式?
+-------+---------+------+-------+
| CONN | TABLE | COLS | OWNER |
+-------+---------+------+-------+
| ONE | TABLE_A | 10 | MIKE |
| ONE | TABLE_B | 9 | MIKE |
| ONE | TAB_A | 11 | KIM |
| ONE | TAB_B | 14 | KIM |
| TWO | TABLE_A | 9 | MIKE |
| TWO | TABLE_B | 9 | MIKE |
| TWO | TAB_A | 11 | KIM |
| TWO | TAB_D | 56 | KIM |
| THREE | TABLE_A | 9 | MIKE |
| THREE | TABLE_C | 3 | MIKE |
| THREE | TABLE_D | 11 | KIM |
| THREE | TAB_A | 11 | KIM |
+-------+---------+------+-------+
我想康恩和業主比較表及其同事。我怎樣才能重塑這個數據做出這種比較?我的數據是在這裏:
dat <- structure(list(CONN = c("ONE", "ONE", "ONE", "ONE", "TWO", "TWO",
"TWO", "TWO", "THREE", "THREE", "THREE", "THREE"), TABLE = c("TABLE_A",
"TABLE_B", "TAB_A", "TAB_B", "TABLE_A", "TABLE_B", "TAB_A", "TAB_D",
"TABLE_A", "TABLE_C", "TABLE_D", "TAB_A"), COLS = c(10L, 9L,
11L, 14L, 9L, 9L, 11L, 56L, 9L, 3L, 11L, 11L), OWNER = c("MIKE",
"MIKE", "KIM", "KIM", "MIKE", "MIKE", "KIM", "KIM", "MIKE", "MIKE",
"KIM", "KIM")), .Names = c("CONN", "TABLE", "COLS", "OWNER"), class = "data.frame", row.names = c(NA,
-12L))
我想是這樣的:
reshape(dat, varying=c('TABLE', 'COLS'), v.names=C('CONN', 'OWNER'), direction='long')
Error in C("CONN", "OWNER") : object not interpretable as a factor
你能告訴我們你想要什麼表? – statquant
我通常會發現'reshape2'更直觀:在你想類似'dcast(CONN + OWNER〜表,數據= DAT,value.var = 「COLS」)'什麼? –
感謝@VincentZoonekynd這並獲得成功: dcast(所有者+ CONN〜表,數據=克,value.var =「COLS」) – Matkrupp