Purpose
To use reshape package on ff package to get a handle on melt and cast functions
> library(reshape)
> head(hdata)
Date ABAN ABB ACC
1 01-07-2009 893.80 780.10 763.15
2 02-07-2009 866.90 768.15 762.55
3 03-07-2009 887.95 784.70 768.70
4 06-07-2009 808.55 744.65 723.85
5 07-07-2009 789.50 752.60 765.00
6 08-07-2009 704.20 713.50 787.60
> colnames(hdata)
[1] "Date" "ABAN" "ABB" "ACC" |
Basic cast operations
> hdata <- hdata[, 1:4]
> temp <- melt(hdata, id = 1)
> colnames(temp) <- c("Date", "stock", "price") |
> cast(temp, . ~ ., length)
value (all)
1 (all) 834 |
> cast(temp, stock ~ ., length)
stock (all)
1 ABAN 278
2 ABB 278
3 ACC 278 |
> cast(temp, stock ~ ., function(x) max(x) - min(x))
stock (all)
1 ABAN 1005.0000
2 ABB 228.6500
3 ACC 314.2500 |
> cast(temp, stock ~ ., mean, trim = 0.1)
stock (all)
1 ABAN 1152.2239
2 ABB 791.6511
3 ACC 848.7815 |
> cast(temp, stock ~ ., sum)
stock (all)
1 ABAN 319776.9
2 ABB 219131.7
3 ACC 236362.3 |
> cast(temp, stock ~ ., sum, margins = "grand_row")
stock (all)
1 ABAN 319776.9
2 ABB 219131.7
3 ACC 236362.3
4 (all) 775271.0 |
> cast(temp, stock ~ ., sum, margins = "grand_col")
stock (all)
1 ABAN 319776.9
2 ABB 219131.7
3 ACC 236362.3 |
> cast(temp, stock ~ ., sum, margins = T)
stock (all)
1 ABAN 319776.9
2 ABB 219131.7
3 ACC 236362.3
4 (all) 775271.0 |
> cast(temp, stock ~ ., summary)
stock Min. X1st.Qu. Median Mean X3rd.Qu. Max.
1 ABAN 645.6 889.4 1192.0 1150.0 1303.0 1651
2 ABB 654.3 750.5 792.4 788.2 837.4 883
3 ACC 699.2 800.5 847.2 850.2 892.6 1014 |
> cast(temp, stock ~ ., quantile, probs = seq(0, 1, 0.1))
stock X0. X10. X20. X30. X40. X50. X60. X70. X80.
1 ABAN 645.60 743.525 857.51 993.435 1161.00 1192.225 1230.24 1259.81 1357.55
2 ABB 654.30 710.695 739.86 757.170 775.85 792.350 807.30 827.48 847.30
3 ACC 699.25 765.420 794.29 811.860 827.64 847.225 862.86 878.01 907.22
X90. X100.
1 1560.295 1650.60
2 860.750 882.95
3 951.140 1013.50 |
> cast(temp, stock ~ ., range)
stock X1 X2
1 ABAN 645.60 1650.60
2 ABB 654.30 882.95
3 ACC 699.25 1013.50 |
> cast(temp, stock ~ ., c(min, max))
stock min max
1 ABAN 645.60 1650.60
2 ABB 654.30 882.95
3 ACC 699.25 1013.50 |
Now look at the french fries data
> data <- french_fries
> datam <- melt(data, id = 1:4, na.rm = T)
> colnames(datam)
[1] "time" "treatment" "subject" "rep" "variable" "value"
> cast(datam, subject ~ time, function(x) 30 - length(x), margins = T)
subject 1 2 3 4 5 6 7 8 9 10 (all)
1 3 0 0 0 0 0 0 0 0 0 30 -240
2 10 0 0 0 0 0 0 0 0 0 0 -270
3 15 0 0 0 0 5 0 0 0 0 0 -265
4 16 0 0 0 0 0 0 0 1 0 0 -269
5 19 0 0 0 0 0 0 0 0 0 0 -270
6 31 0 0 0 0 0 0 0 0 30 0 -240
7 51 0 0 0 0 0 0 0 0 0 0 -270
8 52 0 0 0 0 0 0 0 0 0 0 -270
9 63 0 0 0 0 0 0 0 0 0 0 -270
10 78 0 0 0 0 0 0 0 0 0 0 -270
11 79 0 0 0 0 0 0 1 2 0 30 -237
12 86 0 0 0 0 0 0 0 0 30 0 -240
13 (all) -330 -330 -330 -330 -325 -330 -329 -327 -270 -270 -3441 |
Takeaway - Overall an excellent package for pivoting and doing a detailed analysis on a table