[Tokyo.R#28] Rでピボットテーブル

Packages

library(reshape2)
library(xtable)
library(ggplot2)

dcastとmeltを使ってみる

head(tips)

(tips.cast1 <- dcast(tips, sex ~ smoker, mean, 
    value.var = "total_bill"))

(tips.cast2 <- dcast(tips, smoker ~ sex, mean, 
    value.var = "total_bill"))

(tips.cast3 <- dcast(tips, smoker ~ sex, mean, 
    value.var = "total_bill"))

(tips.cast4 <- dcast(tips, time ~ sex, mean, value.var = "total_bill"))

(tips.cast5 <- dcast(tips, time ~ day, mean, value.var = "total_bill"))

(tips.cast6 <- dcast(tips, time + sex ~ day, mean, 
    value.var = "tip"))

(tips.melt1 <- melt(tips, id.vars = c("sex"), 
    measure.vars = c("total_bill", "tip")))

(tips.melt2 <- melt(tips, id.vars = c("sex", "smoker"), 
    measure.vars = c("total_bill", "tip", "size")))
(tips.melt1 <- melt(tips, id.vars = c("sex", "smoker"), 
    measure.vars = c("total_bill", "tip", "size")))

names(airquality) <- tolower(names(airquality))
head(airquality)

(aqm.melt1 <- melt(airquality, id = c("month", 
    "day"), na.rm = TRUE))


(aqm.melt2 <- melt(airquality, id = c("month", 
    "day"), measure.vars = c("ozone", "solar.r"), na.rm = TRUE))


(aqm.cast1 <- dcast(aqm.melt1, month + day ~ variable, 
    sum))

(aqm.cast2 <- dcast(aqm.melt2, month + day ~ variable, 
    function(x) sum(log(x))))


(aqm.cast3 <- dcast(aqm.melt1, month ~ variable, 
    sum))

結果の出力

print(xtable::xtable(tips.cast1), "html")
sex No Yes
1 Female 18.11 17.98
2 Male 19.79 22.28

print(xtable::xtable(tips.cast2), "html")
smoker Female Male
1 No 18.11 19.79
2 Yes 17.98 22.28


print(xtable::xtable(tips.cast3), "html")
smoker Female Male
1 No 18.11 19.79
2 Yes 17.98 22.28

print(xtable::xtable(tips.cast4), "html")
time Female Male
1 Dinner 19.21 21.46
2 Lunch 16.34 18.05


print(xtable::xtable(tips.cast5), "html")
time Fri Sat Sun Thur
1 Dinner 19.66 20.44 21.41 18.78
2 Lunch 12.85 17.66

ggplot2用の整形としても便利

(aqm.cast3.melt <- melt(aqm.cast3, id.vars = "month"))


ggplot(aqm.cast3.melt, aes(x = month, y = value, 
    col = variable)) + geom_point() + geom_line()

plot of chunk unnamed-chunk-4