reshape: Flexibly reshape data

> library(reshape)

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

    rename, round_any
The following object is masked from 'package:dplyr':

    rename

バージョン: 0.8.5


関数名 概略
cast Cast function
colsplit Split a vector into multiple columns
combine_factor Combine factor levels
condense.df Condense a data frame
expand.grid.df Expand grid
french_fries Sensory data from a french fries experiment
funstofun Aggregate multiple functions into a single function
melt Melt
melt.array Melt an array
melt.data.frame Melt a data frame
merge_all Merge all
namerows Name rows
recast Recast
rename Rename
rescaler Rescaler
smiths Demo data describing the Smiths
sort_df Sort data frame
sparseby Apply a Function to a Data Frame split by levels of indices
stamp Stamp
tips Tipping data
uniquedefault Unique default
untable Untable a dataset

cast

Arguments

  • data
  • formula
  • fun.aggregate
  • add.missing
  • value
  • ...
  • margins
  • subset
  • df
  • subset
  • fill
> data("airquality")
> airquality %>% head()
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6
> melt(airquality, id = c("Month", "Day"), na.rm = TRUE) %>% 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   6    Ozone    28
6     5   7    Ozone    23

colsplit

> iris$Species %>% colsplit(split = "sa", names = "aa")
            aa
1         seto
2         seto
3         seto
4         seto
5         seto
6         seto
7         seto
8         seto
9         seto
10        seto
11        seto
12        seto
13        seto
14        seto
15        seto
16        seto
17        seto
18        seto
19        seto
20        seto
21        seto
22        seto
23        seto
24        seto
25        seto
26        seto
27        seto
28        seto
29        seto
30        seto
31        seto
32        seto
33        seto
34        seto
35        seto
36        seto
37        seto
38        seto
39        seto
40        seto
41        seto
42        seto
43        seto
44        seto
45        seto
46        seto
47        seto
48        seto
49        seto
50        seto
51  versicolor
52  versicolor
53  versicolor
54  versicolor
55  versicolor
56  versicolor
57  versicolor
58  versicolor
59  versicolor
60  versicolor
61  versicolor
62  versicolor
63  versicolor
64  versicolor
65  versicolor
66  versicolor
67  versicolor
68  versicolor
69  versicolor
70  versicolor
71  versicolor
72  versicolor
73  versicolor
74  versicolor
75  versicolor
76  versicolor
77  versicolor
78  versicolor
79  versicolor
80  versicolor
81  versicolor
82  versicolor
83  versicolor
84  versicolor
85  versicolor
86  versicolor
87  versicolor
88  versicolor
89  versicolor
90  versicolor
91  versicolor
92  versicolor
93  versicolor
94  versicolor
95  versicolor
96  versicolor
97  versicolor
98  versicolor
99  versicolor
100 versicolor
101  virginica
102  virginica
103  virginica
104  virginica
105  virginica
106  virginica
107  virginica
108  virginica
109  virginica
110  virginica
111  virginica
112  virginica
113  virginica
114  virginica
115  virginica
116  virginica
117  virginica
118  virginica
119  virginica
120  virginica
121  virginica
122  virginica
123  virginica
124  virginica
125  virginica
126  virginica
127  virginica
128  virginica
129  virginica
130  virginica
131  virginica
132  virginica
133  virginica
134  virginica
135  virginica
136  virginica
137  virginica
138  virginica
139  virginica
140  virginica
141  virginica
142  virginica
143  virginica
144  virginica
145  virginica
146  virginica
147  virginica
148  virginica
149  virginica
150  virginica

combine_factor

要因の組み合わせ

Arguments

  • fac
  • variable
  • other.lavel
> df <- data.frame(a = LETTERS[sample(5, 15, replace=TRUE)], y = rnorm(15))
> df$a
 [1] E A B A E C E B B C D C B E D
Levels: A B C D E
> combine_factor(df$a, variable = c(1, 2, 2, 1, 2))
 [1] B A B A B B B B B B A B B B A
Levels: A B

melt

Arguments

  • data
  • ...
> data("airquality")
> airquality %>% head()
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6
> melt(airquality, id = c("Month", "Day"), na.rm = TRUE) %>% 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   6    Ozone    28
6     5   7    Ozone    23