reshape2: Flexibly Reshape Data: A Reboot of the Reshape Package.

データ整形ツール(Rでピボットテーブルのような操作)

> library(reshape2)

Attaching package: 'reshape2'
The following objects are masked from 'package:reshape':

    colsplit, melt, recast
> data("tips")

バージョン: 1.4.1


関数名 概略
add_margins Add margins to a data frame.
cast Cast functions Cast a molten data frame into an array or data frame.
colsplit Split a vector into multiple columns
french_fries Sensory data from a french fries experiment.
melt Convert an object into a molten data frame.
melt.array Melt an array.
melt.data.frame Melt a data frame into form suitable for easy casting.
melt.default Melt a vector. For vectors, makes a column of a data frame
melt.list Melt a list by recursively melting each component.
melt_check Check that input variables to melt are appropriate.
parse_formula Parse casting formulae.
recast Recast: melt and cast in a single step
smiths Demo data describing the Smiths.
tips Tipping data

add_margins

cast / dcast / acast

Arguments

  • data
  • formula
  • ...
  • margins
  • subset
  • fill
  • drop
  • value.var

melt

データフレーム、配列や行列、リストに対応

Arguments

  • data
  • ...
    • id.vars
    • measure.vars
    • factorsAsStrings
  • na.rm
  • value.name
> melt(data          = airquality, 
+      id.vars       = c("Month", "Day"),
+      variable.name = "Factor", # 新たな列の名前
+      value.name    = "Value") %>% # 新たな列の名前
+   head()
  Month Day variable value
1     5   1    Ozone    41
2     5   2    Ozone    36
3     5   3    Ozone    12
4     5   4    Ozone    18
5     5   5    Ozone    NA
6     5   6    Ozone    28

tips

> tips %>% {
+   print(class(.))
+   dplyr::tbl_df(.)
+ }
[1] "data.frame"
# A tibble: 244 x 7
   total_bill   tip    sex smoker    day   time  size
*       <dbl> <dbl> <fctr> <fctr> <fctr> <fctr> <int>
1       16.99  1.01 Female     No    Sun Dinner     2
2       10.34  1.66   Male     No    Sun Dinner     3
3       21.01  3.50   Male     No    Sun Dinner     3
4       23.68  3.31   Male     No    Sun Dinner     2
5       24.59  3.61 Female     No    Sun Dinner     4
6       25.29  4.71   Male     No    Sun Dinner     4
7        8.77  2.00   Male     No    Sun Dinner     2
8       26.88  3.12   Male     No    Sun Dinner     4
9       15.04  1.96   Male     No    Sun Dinner     2
10      14.78  3.23   Male     No    Sun Dinner     2
# ... with 234 more rows