1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Example 1 - the melt function
library(reshape2)
# toy (wide) dataframe in the reshape2 package
smiths
# columns 2, 3, 4, 5 are measurements, 1 is key
melt(smiths, id = 1)
# columns 3, 4, 5 are measurements, 1,2 are key
melt(smiths, id = c(1, 2))
# Example 2 - the tips dataset
tips$total.bill = tips$total_bill
qplot(total.bill,
tip,
facets = sex~time,
size = I(1.5),
data = tips)
# Example 2 - analyzing tips and bills
head(tips) # first six rows
tipsm = melt(tips,
id = c("sex","smoker","day","time","size"))
head(tipsm) # first six rows
tail(tipsm) # last six rows
# Mean of measurement variables broken by sex.
# Note the role of mean as the aggregating function.
dcast(tipsm,
sex~variable,
fun.aggregate = mean)
# Number of occurrences for measurement variables broken by sex.
# Note the role of length as the aggregating function.
dcast(tipsm,
sex~variable,
fun.aggregate = length)
# Average total bill and tip for different times
dcast(tipsm,
time~variable,
fun.aggregate = mean)
# Similar to above with breakdown for sex and time:
dcast(tipsm,
sex+time~variable,
fun.aggregate = length)
# Similar to above, but with mean and added margins
dcast(tipsm,
sex+time~variable,
fun.aggregate = mean,
margins = TRUE)
Enter to Rename, Shift+Enter to Preview