I have a .csv file like this:
+-------+---------+------+-------+
| 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 |
+-------+---------+------+-------+
I want to compare tables and cols by conn and owner. How can I reshape this data to make this comparison? My data is here:
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))
I tried something like:
reshape(dat, varying=c('TABLE', 'COLS'), v.names=C('CONN', 'OWNER'), direction='long')
Error in C("CONN", "OWNER") : object not interpretable as a factor
I usually find the
reshape2
package more intuitive: just put the desired rows (resp. columns) before (resp. after) the~
.All those CAPITAL column names have made you a bit sticky on the shift key - you did
C('CONN','OWNER')
. Lower-casec
works like this:The
gather()
function from thetidyr
package may also be used: