Purpose
To explore reshape package and see what need it fills
What are the available stuff in base package ?
> handedness <- read.csv("http://news.mrdwab.com/handedness")
> table(handedness)
Handedness
Gender left-handed right-handed
Female 1 5
Male 2 4 |
> set.seed(123)
> fav.col <- sample(c("red", "green", "blue"), 12, replace = T)
> set.seed(123)
> fav.shape <- sample(c("square", "triangle", "circle"), 12, replace = T)
> handedness.plus <- cbind(handedness, fav.col, fav.shape) |
Using table
> table(handedness.plus$fav.col, handedness.plus$Gender, handedness.plus$Handedness)
, , = left-handed
Female Male
blue 0 1
green 1 1
red 0 0
, , = right-handed
Female Male
blue 2 2
green 2 1
red 1 1 |
Using xtabs
> xtabs(~fav.col + Gender + Handedness, handedness.plus)
, , Handedness = left-handed
Gender
fav.col Female Male
blue 0 1
green 1 1
red 0 0
, , Handedness = right-handed
Gender
fav.col Female Male
blue 2 2
green 2 1
red 1 1 |
Using ftable
> ftable(handedness.plus, row.vars = 3, col.vars = c(1, 2))
Gender Female Male
Handedness left-handed right-handed left-handed right-handed
fav.col
blue 0 2 1 2
green 1 2 1 1
red 0 1 0 1 |
Another variation
> ftable(handedness.plus, row.vars = 1, col.vars = c(2, 4))
Handedness left-handed right-handed
fav.shape circle square triangle circle square triangle
Gender
Female 0 0 1 2 1 2
Male 1 0 1 2 1 1 |
Another variation
> ftable(handedness.plus, row.vars = 1:3, col.vars = 4)
fav.shape circle square triangle
Gender Handedness fav.col
Female left-handed blue 0 0 0
green 0 0 1
red 0 0 0
right-handed blue 2 0 0
green 0 0 2
red 0 1 0
Male left-handed blue 1 0 0
green 0 0 1
red 0 0 0
right-handed blue 2 0 0
green 0 0 1
red 0 1 0 |
Using reshape package
> book.sales <- read.csv("http://news.mrdwab.com/booksales")
> head(book.sales)
Representative Region Month Publisher Subject Sales Margin Quantity
1 Raj S 1997-01-01 SAGE Management 135.0 63.45 9
2 Raj S 1997-01-01 Routledge Statistics 120.0 48.00 6
3 Raj S 1997-01-01 Penguin Economics 54.0 22.68 4
4 Raj S 1997-01-01 Routledge Fiction 234.0 128.70 9
5 Soni S 1997-01-01 Penguin Politics 54.0 22.68 4
6 Soni S 1997-01-01 SAGE Politics 185.4 100.12 12 |
> m.book.sales <- melt(book.sales, id.vars = 1:5)
> cast(m.book.sales, Region ~ variable)
Region Sales Margin Quantity
1 E 15 15 15
2 N 122 122 122
3 S 91 91 91
4 W 77 77 77 |
> cast(m.book.sales, Region ~ variable, sum)
Region Sales Margin Quantity
1 E 2267.0 1081.84 135
2 N 18328.3 8552.22 1130
3 S 13276.5 6230.62 813
4 W 12078.8 5758.08 690 |
> cast(m.book.sales, Region + Representative ~ variable, sum)
Region Representative Sales Margin Quantity
1 E Kunj 1309.00 606.37 82
2 E Rajesh 958.00 475.47 53
3 N Gajanan 5348.95 2551.27 307
4 N Mallesh 6931.70 3273.15 445
5 N Priya 4790.65 2168.95 309
6 N Ravi 1257.00 558.85 69
7 S Mahanta 3780.70 1698.01 239
8 S Raj 3463.80 1690.53 183
9 S Soni 6032.00 2842.08 391
10 W Shreekanth 4065.20 1930.84 222
11 W Shreerang 3570.20 1675.55 213
12 W Sree Hari 4443.40 2151.69 255 |
> cast(m.book.sales, Region ~ variable, c(sum, mean))
Region Sales_sum Sales_mean Margin_sum Margin_mean Quantity_sum Quantity_mean
1 E 2267.0 151.1333 1081.84 72.12267 135 9.000000
2 N 18328.3 150.2320 8552.22 70.10016 1130 9.262295
3 S 13276.5 145.8956 6230.62 68.46835 813 8.934066
4 W 12078.8 156.8675 5758.08 74.78026 690 8.961039 |
> cast(m.book.sales, Region + Representative ~ variable, c(length,
+ sum, mean), subset = variable %in% "Sales")
Region Representative Sales_length Sales_sum Sales_mean
1 E Kunj 8 1309.00 163.6250
2 E Rajesh 7 958.00 136.8571
3 N Gajanan 33 5348.95 162.0894
4 N Mallesh 47 6931.70 147.4830
5 N Priya 35 4790.65 136.8757
6 N Ravi 7 1257.00 179.5714
7 S Mahanta 24 3780.70 157.5292
8 S Raj 23 3463.80 150.6000
9 S Soni 44 6032.00 137.0909
10 W Shreekanth 26 4065.20 156.3538
11 W Shreerang 22 3570.20 162.2818
12 W Sree Hari 29 4443.40 153.2207 |
> cast(m.book.sales, Region + Representative ~ variable, c(sum,
+ mean), subset = variable %in% c("Sales", "Quantity"))
Region Representative Sales_sum Sales_mean Quantity_sum Quantity_mean
1 E Kunj 1309.00 163.6250 82 10.250000
2 E Rajesh 958.00 136.8571 53 7.571429
3 N Gajanan 5348.95 162.0894 307 9.303030
4 N Mallesh 6931.70 147.4830 445 9.468085
5 N Priya 4790.65 136.8757 309 8.828571
6 N Ravi 1257.00 179.5714 69 9.857143
7 S Mahanta 3780.70 157.5292 239 9.958333
8 S Raj 3463.80 150.6000 183 7.956522
9 S Soni 6032.00 137.0909 391 8.886364
10 W Shreekanth 4065.20 156.3538 222 8.538462
11 W Shreerang 3570.20 162.2818 213 9.681818
12 W Sree Hari 4443.40 153.2207 255 8.793103 |
Wonderful…Pivot becomes a useless commodity!