tidyr ํจ์บ์ง์ ๋ํ์ ์ธ ํจ์ gather, spread, seperate, unite์ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ดํดํ๊ณ ์ ์ฉํ์ฌ ๋ณด์.
In:
library(dplyr)
library(tidyr)
df_iris = iris
df_iris$id = 1:nrow(df_iris)
df_iris = df_iris[, c(6, 1:5)]
str(df_iris)
Out:
'data.frame': 150 obs. of 6 variables:
$ id : int 1 2 3 4 5 6 7 8 9 10 ...
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
โท ๊ธฐ์กด Iris ๋ฐ์ดํฐ์ id ์ด์ ์ถ๊ฐํ์๋ค. ์ด๋ฅผ ์คํ ๋ฐ์ดํฐ๋ก ์ฌ์ฉํ ๊ฒ์ด๋ค.
โก gather
In:
df_iris_long = df_iris %>%
gather(key = key, value = value, Sepal.Length:Petal.Width)
print(df_iris_long)
Out:
id Species key value
1 1 setosa Sepal.Length 5.1
2 2 setosa Sepal.Length 4.9
3 3 setosa Sepal.Length 4.7
4 4 setosa Sepal.Length 4.6
5 5 setosa Sepal.Length 5.0
6 6 setosa Sepal.Length 5.4
7 7 setosa Sepal.Length 4.6
8 8 setosa Sepal.Length 5.0
9 9 setosa Sepal.Length 4.4
10 10 setosa Sepal.Length 4.9
11 11 setosa Sepal.Length 5.4
12 12 setosa Sepal.Length 4.8
13 13 setosa Sepal.Length 4.8
14 14 setosa Sepal.Length 4.3
15 15 setosa Sepal.Length 5.8
16 16 setosa Sepal.Length 5.7
17 17 setosa Sepal.Length 5.4
18 18 setosa Sepal.Length 5.1
19 19 setosa Sepal.Length 5.7
20 20 setosa Sepal.Length 5.1
21 21 setosa Sepal.Length 5.4
22 22 setosa Sepal.Length 5.1
23 23 setosa Sepal.Length 4.6
24 24 setosa Sepal.Length 5.1
25 25 setosa Sepal.Length 4.8
26 26 setosa Sepal.Length 5.0
27 27 setosa Sepal.Length 5.0
28 28 setosa Sepal.Length 5.2
29 29 setosa Sepal.Length 5.2
30 30 setosa Sepal.Length 4.7
31 31 setosa Sepal.Length 4.8
32 32 setosa Sepal.Length 5.4
33 33 setosa Sepal.Length 5.2
34 34 setosa Sepal.Length 5.5
35 35 setosa Sepal.Length 4.9
36 36 setosa Sepal.Length 5.0
37 37 setosa Sepal.Length 5.5
38 38 setosa Sepal.Length 4.9
39 39 setosa Sepal.Length 4.4
40 40 setosa Sepal.Length 5.1
41 41 setosa Sepal.Length 5.0
42 42 setosa Sepal.Length 4.5
43 43 setosa Sepal.Length 4.4
44 44 setosa Sepal.Length 5.0
45 45 setosa Sepal.Length 5.1
46 46 setosa Sepal.Length 4.8
47 47 setosa Sepal.Length 5.1
48 48 setosa Sepal.Length 4.6
49 49 setosa Sepal.Length 5.3
50 50 setosa Sepal.Length 5.0
51 51 versicolor Sepal.Length 7.0
52 52 versicolor Sepal.Length 6.4
53 53 versicolor Sepal.Length 6.9
54 54 versicolor Sepal.Length 5.5
55 55 versicolor Sepal.Length 6.5
56 56 versicolor Sepal.Length 5.7
57 57 versicolor Sepal.Length 6.3
58 58 versicolor Sepal.Length 4.9
59 59 versicolor Sepal.Length 6.6
60 60 versicolor Sepal.Length 5.2
61 61 versicolor Sepal.Length 5.0
62 62 versicolor Sepal.Length 5.9
63 63 versicolor Sepal.Length 6.0
64 64 versicolor Sepal.Length 6.1
65 65 versicolor Sepal.Length 5.6
66 66 versicolor Sepal.Length 6.7
67 67 versicolor Sepal.Length 5.6
68 68 versicolor Sepal.Length 5.8
69 69 versicolor Sepal.Length 6.2
70 70 versicolor Sepal.Length 5.6
71 71 versicolor Sepal.Length 5.9
72 72 versicolor Sepal.Length 6.1
73 73 versicolor Sepal.Length 6.3
74 74 versicolor Sepal.Length 6.1
75 75 versicolor Sepal.Length 6.4
76 76 versicolor Sepal.Length 6.6
77 77 versicolor Sepal.Length 6.8
78 78 versicolor Sepal.Length 6.7
79 79 versicolor Sepal.Length 6.0
80 80 versicolor Sepal.Length 5.7
81 81 versicolor Sepal.Length 5.5
82 82 versicolor Sepal.Length 5.5
83 83 versicolor Sepal.Length 5.8
84 84 versicolor Sepal.Length 6.0
85 85 versicolor Sepal.Length 5.4
86 86 versicolor Sepal.Length 6.0
87 87 versicolor Sepal.Length 6.7
88 88 versicolor Sepal.Length 6.3
89 89 versicolor Sepal.Length 5.6
90 90 versicolor Sepal.Length 5.5
91 91 versicolor Sepal.Length 5.5
92 92 versicolor Sepal.Length 6.1
93 93 versicolor Sepal.Length 5.8
94 94 versicolor Sepal.Length 5.0
95 95 versicolor Sepal.Length 5.6
96 96 versicolor Sepal.Length 5.7
97 97 versicolor Sepal.Length 5.7
98 98 versicolor Sepal.Length 6.2
99 99 versicolor Sepal.Length 5.1
100 100 versicolor Sepal.Length 5.7
101 101 virginica Sepal.Length 6.3
102 102 virginica Sepal.Length 5.8
103 103 virginica Sepal.Length 7.1
104 104 virginica Sepal.Length 6.3
105 105 virginica Sepal.Length 6.5
106 106 virginica Sepal.Length 7.6
107 107 virginica Sepal.Length 4.9
108 108 virginica Sepal.Length 7.3
109 109 virginica Sepal.Length 6.7
110 110 virginica Sepal.Length 7.2
111 111 virginica Sepal.Length 6.5
112 112 virginica Sepal.Length 6.4
113 113 virginica Sepal.Length 6.8
114 114 virginica Sepal.Length 5.7
115 115 virginica Sepal.Length 5.8
116 116 virginica Sepal.Length 6.4
117 117 virginica Sepal.Length 6.5
118 118 virginica Sepal.Length 7.7
119 119 virginica Sepal.Length 7.7
120 120 virginica Sepal.Length 6.0
121 121 virginica Sepal.Length 6.9
122 122 virginica Sepal.Length 5.6
123 123 virginica Sepal.Length 7.7
124 124 virginica Sepal.Length 6.3
125 125 virginica Sepal.Length 6.7
126 126 virginica Sepal.Length 7.2
127 127 virginica Sepal.Length 6.2
128 128 virginica Sepal.Length 6.1
129 129 virginica Sepal.Length 6.4
130 130 virginica Sepal.Length 7.2
131 131 virginica Sepal.Length 7.4
132 132 virginica Sepal.Length 7.9
133 133 virginica Sepal.Length 6.4
134 134 virginica Sepal.Length 6.3
135 135 virginica Sepal.Length 6.1
136 136 virginica Sepal.Length 7.7
137 137 virginica Sepal.Length 6.3
138 138 virginica Sepal.Length 6.4
139 139 virginica Sepal.Length 6.0
140 140 virginica Sepal.Length 6.9
141 141 virginica Sepal.Length 6.7
142 142 virginica Sepal.Length 6.9
143 143 virginica Sepal.Length 5.8
144 144 virginica Sepal.Length 6.8
145 145 virginica Sepal.Length 6.7
146 146 virginica Sepal.Length 6.7
147 147 virginica Sepal.Length 6.3
148 148 virginica Sepal.Length 6.5
149 149 virginica Sepal.Length 6.2
150 150 virginica Sepal.Length 5.9
151 1 setosa Sepal.Width 3.5
152 2 setosa Sepal.Width 3.0
153 3 setosa Sepal.Width 3.2
154 4 setosa Sepal.Width 3.1
155 5 setosa Sepal.Width 3.6
156 6 setosa Sepal.Width 3.9
157 7 setosa Sepal.Width 3.4
158 8 setosa Sepal.Width 3.4
159 9 setosa Sepal.Width 2.9
160 10 setosa Sepal.Width 3.1
161 11 setosa Sepal.Width 3.7
162 12 setosa Sepal.Width 3.4
163 13 setosa Sepal.Width 3.0
164 14 setosa Sepal.Width 3.0
165 15 setosa Sepal.Width 4.0
166 16 setosa Sepal.Width 4.4
167 17 setosa Sepal.Width 3.9
168 18 setosa Sepal.Width 3.5
169 19 setosa Sepal.Width 3.8
170 20 setosa Sepal.Width 3.8
171 21 setosa Sepal.Width 3.4
172 22 setosa Sepal.Width 3.7
173 23 setosa Sepal.Width 3.6
174 24 setosa Sepal.Width 3.3
175 25 setosa Sepal.Width 3.4
176 26 setosa Sepal.Width 3.0
177 27 setosa Sepal.Width 3.4
178 28 setosa Sepal.Width 3.5
179 29 setosa Sepal.Width 3.4
180 30 setosa Sepal.Width 3.2
181 31 setosa Sepal.Width 3.1
182 32 setosa Sepal.Width 3.4
183 33 setosa Sepal.Width 4.1
184 34 setosa Sepal.Width 4.2
185 35 setosa Sepal.Width 3.1
186 36 setosa Sepal.Width 3.2
187 37 setosa Sepal.Width 3.5
188 38 setosa Sepal.Width 3.6
189 39 setosa Sepal.Width 3.0
190 40 setosa Sepal.Width 3.4
191 41 setosa Sepal.Width 3.5
192 42 setosa Sepal.Width 2.3
193 43 setosa Sepal.Width 3.2
194 44 setosa Sepal.Width 3.5
195 45 setosa Sepal.Width 3.8
196 46 setosa Sepal.Width 3.0
197 47 setosa Sepal.Width 3.8
198 48 setosa Sepal.Width 3.2
199 49 setosa Sepal.Width 3.7
200 50 setosa Sepal.Width 3.3
201 51 versicolor Sepal.Width 3.2
202 52 versicolor Sepal.Width 3.2
203 53 versicolor Sepal.Width 3.1
204 54 versicolor Sepal.Width 2.3
205 55 versicolor Sepal.Width 2.8
206 56 versicolor Sepal.Width 2.8
207 57 versicolor Sepal.Width 3.3
208 58 versicolor Sepal.Width 2.4
209 59 versicolor Sepal.Width 2.9
210 60 versicolor Sepal.Width 2.7
211 61 versicolor Sepal.Width 2.0
212 62 versicolor Sepal.Width 3.0
213 63 versicolor Sepal.Width 2.2
214 64 versicolor Sepal.Width 2.9
215 65 versicolor Sepal.Width 2.9
216 66 versicolor Sepal.Width 3.1
217 67 versicolor Sepal.Width 3.0
218 68 versicolor Sepal.Width 2.7
219 69 versicolor Sepal.Width 2.2
220 70 versicolor Sepal.Width 2.5
221 71 versicolor Sepal.Width 3.2
222 72 versicolor Sepal.Width 2.8
223 73 versicolor Sepal.Width 2.5
224 74 versicolor Sepal.Width 2.8
225 75 versicolor Sepal.Width 2.9
226 76 versicolor Sepal.Width 3.0
227 77 versicolor Sepal.Width 2.8
228 78 versicolor Sepal.Width 3.0
229 79 versicolor Sepal.Width 2.9
230 80 versicolor Sepal.Width 2.6
231 81 versicolor Sepal.Width 2.4
232 82 versicolor Sepal.Width 2.4
233 83 versicolor Sepal.Width 2.7
234 84 versicolor Sepal.Width 2.7
235 85 versicolor Sepal.Width 3.0
236 86 versicolor Sepal.Width 3.4
237 87 versicolor Sepal.Width 3.1
238 88 versicolor Sepal.Width 2.3
239 89 versicolor Sepal.Width 3.0
240 90 versicolor Sepal.Width 2.5
241 91 versicolor Sepal.Width 2.6
242 92 versicolor Sepal.Width 3.0
243 93 versicolor Sepal.Width 2.6
244 94 versicolor Sepal.Width 2.3
245 95 versicolor Sepal.Width 2.7
246 96 versicolor Sepal.Width 3.0
247 97 versicolor Sepal.Width 2.9
248 98 versicolor Sepal.Width 2.9
249 99 versicolor Sepal.Width 2.5
250 100 versicolor Sepal.Width 2.8
[ reached 'max' / getOption("max.print") -- omitted 350 rows ]
โท gather ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ๊ฐ์ ์ด์ ๋ ๊ฐ์ ์ด๋ก ์ค์ผ ์ ์๋ค. ์ฒซ ๋ฒ์งธ ์ด์ gather ํจ์๋ฅผ ์ ์ฉํ๋ ์ด์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๊ณ , ๋ ๋ฒ์งธ ์ด์ ํด๋น ์ด์ ๊ฐ์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ค. ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ค์ํผ Sepal.Length ์ด๋ถํฐ Petal.Width ์ด๊น์ง์ ์ ๋ณด๊ฐ key ์ด์ ๋ค์ด๊ฐ ์๊ณ , value ์ด์๋ ํด๋น ์ด์ ์ ๋ณด๊ฐ ๋ค์ด๊ฐ ์๋ค.
โท key์ value ์ธ์๋ฅผ ํตํด ์๋ก ๋ง๋ค์ด์ง ๋ ๊ฐ์ ์ด์ ์ด๋ฆ์ ์ง์ ํ ์ ์๋ค. ๋ง์ง๋ง ์ธ์๋ฅผ ํตํด ํฉ์น๊ณ ์ ํ๋ ์ด์ ์ค์ ํ ์ ์๋ค.
โก spread
In:
df_iris_wide = df_iris_long %>%
spread(key = key, value = value)
print(df_iris_wide)
Out:
id Species Petal.Length Petal.Width Sepal.Length Sepal.Width
1 1 setosa 1.4 0.2 5.1 3.5
2 2 setosa 1.4 0.2 4.9 3.0
3 3 setosa 1.3 0.2 4.7 3.2
4 4 setosa 1.5 0.2 4.6 3.1
5 5 setosa 1.4 0.2 5.0 3.6
6 6 setosa 1.7 0.4 5.4 3.9
7 7 setosa 1.4 0.3 4.6 3.4
8 8 setosa 1.5 0.2 5.0 3.4
9 9 setosa 1.4 0.2 4.4 2.9
10 10 setosa 1.5 0.1 4.9 3.1
11 11 setosa 1.5 0.2 5.4 3.7
12 12 setosa 1.6 0.2 4.8 3.4
13 13 setosa 1.4 0.1 4.8 3.0
14 14 setosa 1.1 0.1 4.3 3.0
15 15 setosa 1.2 0.2 5.8 4.0
16 16 setosa 1.5 0.4 5.7 4.4
17 17 setosa 1.3 0.4 5.4 3.9
18 18 setosa 1.4 0.3 5.1 3.5
19 19 setosa 1.7 0.3 5.7 3.8
20 20 setosa 1.5 0.3 5.1 3.8
21 21 setosa 1.7 0.2 5.4 3.4
22 22 setosa 1.5 0.4 5.1 3.7
23 23 setosa 1.0 0.2 4.6 3.6
24 24 setosa 1.7 0.5 5.1 3.3
25 25 setosa 1.9 0.2 4.8 3.4
26 26 setosa 1.6 0.2 5.0 3.0
27 27 setosa 1.6 0.4 5.0 3.4
28 28 setosa 1.5 0.2 5.2 3.5
29 29 setosa 1.4 0.2 5.2 3.4
30 30 setosa 1.6 0.2 4.7 3.2
31 31 setosa 1.6 0.2 4.8 3.1
32 32 setosa 1.5 0.4 5.4 3.4
33 33 setosa 1.5 0.1 5.2 4.1
34 34 setosa 1.4 0.2 5.5 4.2
35 35 setosa 1.5 0.2 4.9 3.1
36 36 setosa 1.2 0.2 5.0 3.2
37 37 setosa 1.3 0.2 5.5 3.5
38 38 setosa 1.4 0.1 4.9 3.6
39 39 setosa 1.3 0.2 4.4 3.0
40 40 setosa 1.5 0.2 5.1 3.4
41 41 setosa 1.3 0.3 5.0 3.5
42 42 setosa 1.3 0.3 4.5 2.3
43 43 setosa 1.3 0.2 4.4 3.2
44 44 setosa 1.6 0.6 5.0 3.5
45 45 setosa 1.9 0.4 5.1 3.8
46 46 setosa 1.4 0.3 4.8 3.0
47 47 setosa 1.6 0.2 5.1 3.8
48 48 setosa 1.4 0.2 4.6 3.2
49 49 setosa 1.5 0.2 5.3 3.7
50 50 setosa 1.4 0.2 5.0 3.3
51 51 versicolor 4.7 1.4 7.0 3.2
52 52 versicolor 4.5 1.5 6.4 3.2
53 53 versicolor 4.9 1.5 6.9 3.1
54 54 versicolor 4.0 1.3 5.5 2.3
55 55 versicolor 4.6 1.5 6.5 2.8
56 56 versicolor 4.5 1.3 5.7 2.8
57 57 versicolor 4.7 1.6 6.3 3.3
58 58 versicolor 3.3 1.0 4.9 2.4
59 59 versicolor 4.6 1.3 6.6 2.9
60 60 versicolor 3.9 1.4 5.2 2.7
61 61 versicolor 3.5 1.0 5.0 2.0
62 62 versicolor 4.2 1.5 5.9 3.0
63 63 versicolor 4.0 1.0 6.0 2.2
64 64 versicolor 4.7 1.4 6.1 2.9
65 65 versicolor 3.6 1.3 5.6 2.9
66 66 versicolor 4.4 1.4 6.7 3.1
67 67 versicolor 4.5 1.5 5.6 3.0
68 68 versicolor 4.1 1.0 5.8 2.7
69 69 versicolor 4.5 1.5 6.2 2.2
70 70 versicolor 3.9 1.1 5.6 2.5
71 71 versicolor 4.8 1.8 5.9 3.2
72 72 versicolor 4.0 1.3 6.1 2.8
73 73 versicolor 4.9 1.5 6.3 2.5
74 74 versicolor 4.7 1.2 6.1 2.8
75 75 versicolor 4.3 1.3 6.4 2.9
76 76 versicolor 4.4 1.4 6.6 3.0
77 77 versicolor 4.8 1.4 6.8 2.8
78 78 versicolor 5.0 1.7 6.7 3.0
79 79 versicolor 4.5 1.5 6.0 2.9
80 80 versicolor 3.5 1.0 5.7 2.6
81 81 versicolor 3.8 1.1 5.5 2.4
82 82 versicolor 3.7 1.0 5.5 2.4
83 83 versicolor 3.9 1.2 5.8 2.7
84 84 versicolor 5.1 1.6 6.0 2.7
85 85 versicolor 4.5 1.5 5.4 3.0
86 86 versicolor 4.5 1.6 6.0 3.4
87 87 versicolor 4.7 1.5 6.7 3.1
88 88 versicolor 4.4 1.3 6.3 2.3
89 89 versicolor 4.1 1.3 5.6 3.0
90 90 versicolor 4.0 1.3 5.5 2.5
91 91 versicolor 4.4 1.2 5.5 2.6
92 92 versicolor 4.6 1.4 6.1 3.0
93 93 versicolor 4.0 1.2 5.8 2.6
94 94 versicolor 3.3 1.0 5.0 2.3
95 95 versicolor 4.2 1.3 5.6 2.7
96 96 versicolor 4.2 1.2 5.7 3.0
97 97 versicolor 4.2 1.3 5.7 2.9
98 98 versicolor 4.3 1.3 6.2 2.9
99 99 versicolor 3.0 1.1 5.1 2.5
100 100 versicolor 4.1 1.3 5.7 2.8
101 101 virginica 6.0 2.5 6.3 3.3
102 102 virginica 5.1 1.9 5.8 2.7
103 103 virginica 5.9 2.1 7.1 3.0
104 104 virginica 5.6 1.8 6.3 2.9
105 105 virginica 5.8 2.2 6.5 3.0
106 106 virginica 6.6 2.1 7.6 3.0
107 107 virginica 4.5 1.7 4.9 2.5
108 108 virginica 6.3 1.8 7.3 2.9
109 109 virginica 5.8 1.8 6.7 2.5
110 110 virginica 6.1 2.5 7.2 3.6
111 111 virginica 5.1 2.0 6.5 3.2
112 112 virginica 5.3 1.9 6.4 2.7
113 113 virginica 5.5 2.1 6.8 3.0
114 114 virginica 5.0 2.0 5.7 2.5
115 115 virginica 5.1 2.4 5.8 2.8
116 116 virginica 5.3 2.3 6.4 3.2
117 117 virginica 5.5 1.8 6.5 3.0
118 118 virginica 6.7 2.2 7.7 3.8
119 119 virginica 6.9 2.3 7.7 2.6
120 120 virginica 5.0 1.5 6.0 2.2
121 121 virginica 5.7 2.3 6.9 3.2
122 122 virginica 4.9 2.0 5.6 2.8
123 123 virginica 6.7 2.0 7.7 2.8
124 124 virginica 4.9 1.8 6.3 2.7
125 125 virginica 5.7 2.1 6.7 3.3
126 126 virginica 6.0 1.8 7.2 3.2
127 127 virginica 4.8 1.8 6.2 2.8
128 128 virginica 4.9 1.8 6.1 3.0
129 129 virginica 5.6 2.1 6.4 2.8
130 130 virginica 5.8 1.6 7.2 3.0
131 131 virginica 6.1 1.9 7.4 2.8
132 132 virginica 6.4 2.0 7.9 3.8
133 133 virginica 5.6 2.2 6.4 2.8
134 134 virginica 5.1 1.5 6.3 2.8
135 135 virginica 5.6 1.4 6.1 2.6
136 136 virginica 6.1 2.3 7.7 3.0
137 137 virginica 5.6 2.4 6.3 3.4
138 138 virginica 5.5 1.8 6.4 3.1
139 139 virginica 4.8 1.8 6.0 3.0
140 140 virginica 5.4 2.1 6.9 3.1
141 141 virginica 5.6 2.4 6.7 3.1
142 142 virginica 5.1 2.3 6.9 3.1
143 143 virginica 5.1 1.9 5.8 2.7
144 144 virginica 5.9 2.3 6.8 3.2
145 145 virginica 5.7 2.5 6.7 3.3
146 146 virginica 5.2 2.3 6.7 3.0
147 147 virginica 5.0 1.9 6.3 2.5
148 148 virginica 5.2 2.0 6.5 3.0
149 149 virginica 5.4 2.3 6.2 3.4
150 150 virginica 5.1 1.8 5.9 3.0
โท spread ํจ์๋ ๋ง ๊ทธ๋๋ก ํผ์ณ์ฃผ๋ ์ญํ ์ ํ๋ค. gather ํจ์๋ฅผ ํตํด ๋ ๊ฐ์ ์ด๋ก ์ค์ธ ๊ฒ์ ๋ค์ ์๋๋๋ก ๋๋ ค์ค๋ค. key ์ธ์๋ฅผ ํตํด ํผ์น๊ณ ์ ํ๋ ์ด์ ์ ํํ๊ณ , value ์ธ์๋ฅผ ํตํด ๋ถ๋ฆฌํ ์ด์ ํ ๋นํ๋ ๊ฐ์ ์ ํํ ์ ์๋ค.
โท ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ค์ํผ key ์ด์ ํฌํจ๋ ๊ฐ์ด ์ด๋ก ๋ฐ๋์๊ณ , ํด๋น ์ด์ value๊ฐ ๊ฐ ์ด์ ๊ฐ์ผ๋ก ๋ณํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
โก separate
In:
df_iris_sep = df_iris_long %>%
separate(col = key, into = c('A', 'B'), sep = '\\.')
print(df_iris_sep)
Out:
id Species A B value
1 1 setosa Sepal Length 5.1
2 2 setosa Sepal Length 4.9
3 3 setosa Sepal Length 4.7
4 4 setosa Sepal Length 4.6
5 5 setosa Sepal Length 5.0
6 6 setosa Sepal Length 5.4
7 7 setosa Sepal Length 4.6
8 8 setosa Sepal Length 5.0
9 9 setosa Sepal Length 4.4
10 10 setosa Sepal Length 4.9
11 11 setosa Sepal Length 5.4
12 12 setosa Sepal Length 4.8
13 13 setosa Sepal Length 4.8
14 14 setosa Sepal Length 4.3
15 15 setosa Sepal Length 5.8
16 16 setosa Sepal Length 5.7
17 17 setosa Sepal Length 5.4
18 18 setosa Sepal Length 5.1
19 19 setosa Sepal Length 5.7
20 20 setosa Sepal Length 5.1
21 21 setosa Sepal Length 5.4
22 22 setosa Sepal Length 5.1
23 23 setosa Sepal Length 4.6
24 24 setosa Sepal Length 5.1
25 25 setosa Sepal Length 4.8
26 26 setosa Sepal Length 5.0
27 27 setosa Sepal Length 5.0
28 28 setosa Sepal Length 5.2
29 29 setosa Sepal Length 5.2
30 30 setosa Sepal Length 4.7
31 31 setosa Sepal Length 4.8
32 32 setosa Sepal Length 5.4
33 33 setosa Sepal Length 5.2
34 34 setosa Sepal Length 5.5
35 35 setosa Sepal Length 4.9
36 36 setosa Sepal Length 5.0
37 37 setosa Sepal Length 5.5
38 38 setosa Sepal Length 4.9
39 39 setosa Sepal Length 4.4
40 40 setosa Sepal Length 5.1
41 41 setosa Sepal Length 5.0
42 42 setosa Sepal Length 4.5
43 43 setosa Sepal Length 4.4
44 44 setosa Sepal Length 5.0
45 45 setosa Sepal Length 5.1
46 46 setosa Sepal Length 4.8
47 47 setosa Sepal Length 5.1
48 48 setosa Sepal Length 4.6
49 49 setosa Sepal Length 5.3
50 50 setosa Sepal Length 5.0
51 51 versicolor Sepal Length 7.0
52 52 versicolor Sepal Length 6.4
53 53 versicolor Sepal Length 6.9
54 54 versicolor Sepal Length 5.5
55 55 versicolor Sepal Length 6.5
56 56 versicolor Sepal Length 5.7
57 57 versicolor Sepal Length 6.3
58 58 versicolor Sepal Length 4.9
59 59 versicolor Sepal Length 6.6
60 60 versicolor Sepal Length 5.2
61 61 versicolor Sepal Length 5.0
62 62 versicolor Sepal Length 5.9
63 63 versicolor Sepal Length 6.0
64 64 versicolor Sepal Length 6.1
65 65 versicolor Sepal Length 5.6
66 66 versicolor Sepal Length 6.7
67 67 versicolor Sepal Length 5.6
68 68 versicolor Sepal Length 5.8
69 69 versicolor Sepal Length 6.2
70 70 versicolor Sepal Length 5.6
71 71 versicolor Sepal Length 5.9
72 72 versicolor Sepal Length 6.1
73 73 versicolor Sepal Length 6.3
74 74 versicolor Sepal Length 6.1
75 75 versicolor Sepal Length 6.4
76 76 versicolor Sepal Length 6.6
77 77 versicolor Sepal Length 6.8
78 78 versicolor Sepal Length 6.7
79 79 versicolor Sepal Length 6.0
80 80 versicolor Sepal Length 5.7
81 81 versicolor Sepal Length 5.5
82 82 versicolor Sepal Length 5.5
83 83 versicolor Sepal Length 5.8
84 84 versicolor Sepal Length 6.0
85 85 versicolor Sepal Length 5.4
86 86 versicolor Sepal Length 6.0
87 87 versicolor Sepal Length 6.7
88 88 versicolor Sepal Length 6.3
89 89 versicolor Sepal Length 5.6
90 90 versicolor Sepal Length 5.5
91 91 versicolor Sepal Length 5.5
92 92 versicolor Sepal Length 6.1
93 93 versicolor Sepal Length 5.8
94 94 versicolor Sepal Length 5.0
95 95 versicolor Sepal Length 5.6
96 96 versicolor Sepal Length 5.7
97 97 versicolor Sepal Length 5.7
98 98 versicolor Sepal Length 6.2
99 99 versicolor Sepal Length 5.1
100 100 versicolor Sepal Length 5.7
101 101 virginica Sepal Length 6.3
102 102 virginica Sepal Length 5.8
103 103 virginica Sepal Length 7.1
104 104 virginica Sepal Length 6.3
105 105 virginica Sepal Length 6.5
106 106 virginica Sepal Length 7.6
107 107 virginica Sepal Length 4.9
108 108 virginica Sepal Length 7.3
109 109 virginica Sepal Length 6.7
110 110 virginica Sepal Length 7.2
111 111 virginica Sepal Length 6.5
112 112 virginica Sepal Length 6.4
113 113 virginica Sepal Length 6.8
114 114 virginica Sepal Length 5.7
115 115 virginica Sepal Length 5.8
116 116 virginica Sepal Length 6.4
117 117 virginica Sepal Length 6.5
118 118 virginica Sepal Length 7.7
119 119 virginica Sepal Length 7.7
120 120 virginica Sepal Length 6.0
121 121 virginica Sepal Length 6.9
122 122 virginica Sepal Length 5.6
123 123 virginica Sepal Length 7.7
124 124 virginica Sepal Length 6.3
125 125 virginica Sepal Length 6.7
126 126 virginica Sepal Length 7.2
127 127 virginica Sepal Length 6.2
128 128 virginica Sepal Length 6.1
129 129 virginica Sepal Length 6.4
130 130 virginica Sepal Length 7.2
131 131 virginica Sepal Length 7.4
132 132 virginica Sepal Length 7.9
133 133 virginica Sepal Length 6.4
134 134 virginica Sepal Length 6.3
135 135 virginica Sepal Length 6.1
136 136 virginica Sepal Length 7.7
137 137 virginica Sepal Length 6.3
138 138 virginica Sepal Length 6.4
139 139 virginica Sepal Length 6.0
140 140 virginica Sepal Length 6.9
141 141 virginica Sepal Length 6.7
142 142 virginica Sepal Length 6.9
143 143 virginica Sepal Length 5.8
144 144 virginica Sepal Length 6.8
145 145 virginica Sepal Length 6.7
146 146 virginica Sepal Length 6.7
147 147 virginica Sepal Length 6.3
148 148 virginica Sepal Length 6.5
149 149 virginica Sepal Length 6.2
150 150 virginica Sepal Length 5.9
151 1 setosa Sepal Width 3.5
152 2 setosa Sepal Width 3.0
153 3 setosa Sepal Width 3.2
154 4 setosa Sepal Width 3.1
155 5 setosa Sepal Width 3.6
156 6 setosa Sepal Width 3.9
157 7 setosa Sepal Width 3.4
158 8 setosa Sepal Width 3.4
159 9 setosa Sepal Width 2.9
160 10 setosa Sepal Width 3.1
161 11 setosa Sepal Width 3.7
162 12 setosa Sepal Width 3.4
163 13 setosa Sepal Width 3.0
164 14 setosa Sepal Width 3.0
165 15 setosa Sepal Width 4.0
166 16 setosa Sepal Width 4.4
167 17 setosa Sepal Width 3.9
168 18 setosa Sepal Width 3.5
169 19 setosa Sepal Width 3.8
170 20 setosa Sepal Width 3.8
171 21 setosa Sepal Width 3.4
172 22 setosa Sepal Width 3.7
173 23 setosa Sepal Width 3.6
174 24 setosa Sepal Width 3.3
175 25 setosa Sepal Width 3.4
176 26 setosa Sepal Width 3.0
177 27 setosa Sepal Width 3.4
178 28 setosa Sepal Width 3.5
179 29 setosa Sepal Width 3.4
180 30 setosa Sepal Width 3.2
181 31 setosa Sepal Width 3.1
182 32 setosa Sepal Width 3.4
183 33 setosa Sepal Width 4.1
184 34 setosa Sepal Width 4.2
185 35 setosa Sepal Width 3.1
186 36 setosa Sepal Width 3.2
187 37 setosa Sepal Width 3.5
188 38 setosa Sepal Width 3.6
189 39 setosa Sepal Width 3.0
190 40 setosa Sepal Width 3.4
191 41 setosa Sepal Width 3.5
192 42 setosa Sepal Width 2.3
193 43 setosa Sepal Width 3.2
194 44 setosa Sepal Width 3.5
195 45 setosa Sepal Width 3.8
196 46 setosa Sepal Width 3.0
197 47 setosa Sepal Width 3.8
198 48 setosa Sepal Width 3.2
199 49 setosa Sepal Width 3.7
200 50 setosa Sepal Width 3.3
[ reached 'max' / getOption("max.print") -- omitted 400 rows ]
โท separate ํจ์์ ์ฒซ ๋ฒ์งธ ์ธ์๋ฅผ ํตํด ๋ถ๋ฆฌํ๊ณ ์ ํ๋ ์ด์ ์ ํํ๊ณ , ๋ ๋ฒ์งธ ์ธ์๋ฅผ ํตํด ๋ถ๋ฆฌ๋ ์ด์ ์ด๋ฆ์ ์ง์ ํ๋ค. sep ์ธ์๋ฅผ ํตํด, ์ด์ ๋ถ๋ฆฌ ๊ธฐ์ค์ ์ค์ ํ ์ ์๋ค.
โท ์์ ์ฝ๋๋ฅผ ํตํด ๊ธฐ์กด ๋ฐ์ดํฐํ๋ ์์ key ์ด์ "."์ ๋ถ๋ฆฌ ๊ธฐ์ค์ผ๋ก ์ ์ฉํ์ฌ ๋ถ๋ฆฌ๋ฅผ ์ํํ ๊ฒ์ด๋ค.
โก unite
In:
df_iris_unite = df_iris_sep %>%
unite(col = key, c('A', 'B'), sep = '_')
print(df_iris_unite)
Out:
id Species key value
1 1 setosa Sepal_Length 5.1
2 2 setosa Sepal_Length 4.9
3 3 setosa Sepal_Length 4.7
4 4 setosa Sepal_Length 4.6
5 5 setosa Sepal_Length 5.0
6 6 setosa Sepal_Length 5.4
7 7 setosa Sepal_Length 4.6
8 8 setosa Sepal_Length 5.0
9 9 setosa Sepal_Length 4.4
10 10 setosa Sepal_Length 4.9
11 11 setosa Sepal_Length 5.4
12 12 setosa Sepal_Length 4.8
13 13 setosa Sepal_Length 4.8
14 14 setosa Sepal_Length 4.3
15 15 setosa Sepal_Length 5.8
16 16 setosa Sepal_Length 5.7
17 17 setosa Sepal_Length 5.4
18 18 setosa Sepal_Length 5.1
19 19 setosa Sepal_Length 5.7
20 20 setosa Sepal_Length 5.1
21 21 setosa Sepal_Length 5.4
22 22 setosa Sepal_Length 5.1
23 23 setosa Sepal_Length 4.6
24 24 setosa Sepal_Length 5.1
25 25 setosa Sepal_Length 4.8
26 26 setosa Sepal_Length 5.0
27 27 setosa Sepal_Length 5.0
28 28 setosa Sepal_Length 5.2
29 29 setosa Sepal_Length 5.2
30 30 setosa Sepal_Length 4.7
31 31 setosa Sepal_Length 4.8
32 32 setosa Sepal_Length 5.4
33 33 setosa Sepal_Length 5.2
34 34 setosa Sepal_Length 5.5
35 35 setosa Sepal_Length 4.9
36 36 setosa Sepal_Length 5.0
37 37 setosa Sepal_Length 5.5
38 38 setosa Sepal_Length 4.9
39 39 setosa Sepal_Length 4.4
40 40 setosa Sepal_Length 5.1
41 41 setosa Sepal_Length 5.0
42 42 setosa Sepal_Length 4.5
43 43 setosa Sepal_Length 4.4
44 44 setosa Sepal_Length 5.0
45 45 setosa Sepal_Length 5.1
46 46 setosa Sepal_Length 4.8
47 47 setosa Sepal_Length 5.1
48 48 setosa Sepal_Length 4.6
49 49 setosa Sepal_Length 5.3
50 50 setosa Sepal_Length 5.0
51 51 versicolor Sepal_Length 7.0
52 52 versicolor Sepal_Length 6.4
53 53 versicolor Sepal_Length 6.9
54 54 versicolor Sepal_Length 5.5
55 55 versicolor Sepal_Length 6.5
56 56 versicolor Sepal_Length 5.7
57 57 versicolor Sepal_Length 6.3
58 58 versicolor Sepal_Length 4.9
59 59 versicolor Sepal_Length 6.6
60 60 versicolor Sepal_Length 5.2
61 61 versicolor Sepal_Length 5.0
62 62 versicolor Sepal_Length 5.9
63 63 versicolor Sepal_Length 6.0
64 64 versicolor Sepal_Length 6.1
65 65 versicolor Sepal_Length 5.6
66 66 versicolor Sepal_Length 6.7
67 67 versicolor Sepal_Length 5.6
68 68 versicolor Sepal_Length 5.8
69 69 versicolor Sepal_Length 6.2
70 70 versicolor Sepal_Length 5.6
71 71 versicolor Sepal_Length 5.9
72 72 versicolor Sepal_Length 6.1
73 73 versicolor Sepal_Length 6.3
74 74 versicolor Sepal_Length 6.1
75 75 versicolor Sepal_Length 6.4
76 76 versicolor Sepal_Length 6.6
77 77 versicolor Sepal_Length 6.8
78 78 versicolor Sepal_Length 6.7
79 79 versicolor Sepal_Length 6.0
80 80 versicolor Sepal_Length 5.7
81 81 versicolor Sepal_Length 5.5
82 82 versicolor Sepal_Length 5.5
83 83 versicolor Sepal_Length 5.8
84 84 versicolor Sepal_Length 6.0
85 85 versicolor Sepal_Length 5.4
86 86 versicolor Sepal_Length 6.0
87 87 versicolor Sepal_Length 6.7
88 88 versicolor Sepal_Length 6.3
89 89 versicolor Sepal_Length 5.6
90 90 versicolor Sepal_Length 5.5
91 91 versicolor Sepal_Length 5.5
92 92 versicolor Sepal_Length 6.1
93 93 versicolor Sepal_Length 5.8
94 94 versicolor Sepal_Length 5.0
95 95 versicolor Sepal_Length 5.6
96 96 versicolor Sepal_Length 5.7
97 97 versicolor Sepal_Length 5.7
98 98 versicolor Sepal_Length 6.2
99 99 versicolor Sepal_Length 5.1
100 100 versicolor Sepal_Length 5.7
101 101 virginica Sepal_Length 6.3
102 102 virginica Sepal_Length 5.8
103 103 virginica Sepal_Length 7.1
104 104 virginica Sepal_Length 6.3
105 105 virginica Sepal_Length 6.5
106 106 virginica Sepal_Length 7.6
107 107 virginica Sepal_Length 4.9
108 108 virginica Sepal_Length 7.3
109 109 virginica Sepal_Length 6.7
110 110 virginica Sepal_Length 7.2
111 111 virginica Sepal_Length 6.5
112 112 virginica Sepal_Length 6.4
113 113 virginica Sepal_Length 6.8
114 114 virginica Sepal_Length 5.7
115 115 virginica Sepal_Length 5.8
116 116 virginica Sepal_Length 6.4
117 117 virginica Sepal_Length 6.5
118 118 virginica Sepal_Length 7.7
119 119 virginica Sepal_Length 7.7
120 120 virginica Sepal_Length 6.0
121 121 virginica Sepal_Length 6.9
122 122 virginica Sepal_Length 5.6
123 123 virginica Sepal_Length 7.7
124 124 virginica Sepal_Length 6.3
125 125 virginica Sepal_Length 6.7
126 126 virginica Sepal_Length 7.2
127 127 virginica Sepal_Length 6.2
128 128 virginica Sepal_Length 6.1
129 129 virginica Sepal_Length 6.4
130 130 virginica Sepal_Length 7.2
131 131 virginica Sepal_Length 7.4
132 132 virginica Sepal_Length 7.9
133 133 virginica Sepal_Length 6.4
134 134 virginica Sepal_Length 6.3
135 135 virginica Sepal_Length 6.1
136 136 virginica Sepal_Length 7.7
137 137 virginica Sepal_Length 6.3
138 138 virginica Sepal_Length 6.4
139 139 virginica Sepal_Length 6.0
140 140 virginica Sepal_Length 6.9
141 141 virginica Sepal_Length 6.7
142 142 virginica Sepal_Length 6.9
143 143 virginica Sepal_Length 5.8
144 144 virginica Sepal_Length 6.8
145 145 virginica Sepal_Length 6.7
146 146 virginica Sepal_Length 6.7
147 147 virginica Sepal_Length 6.3
148 148 virginica Sepal_Length 6.5
149 149 virginica Sepal_Length 6.2
150 150 virginica Sepal_Length 5.9
151 1 setosa Sepal_Width 3.5
152 2 setosa Sepal_Width 3.0
153 3 setosa Sepal_Width 3.2
154 4 setosa Sepal_Width 3.1
155 5 setosa Sepal_Width 3.6
156 6 setosa Sepal_Width 3.9
157 7 setosa Sepal_Width 3.4
158 8 setosa Sepal_Width 3.4
159 9 setosa Sepal_Width 2.9
160 10 setosa Sepal_Width 3.1
161 11 setosa Sepal_Width 3.7
162 12 setosa Sepal_Width 3.4
163 13 setosa Sepal_Width 3.0
164 14 setosa Sepal_Width 3.0
165 15 setosa Sepal_Width 4.0
166 16 setosa Sepal_Width 4.4
167 17 setosa Sepal_Width 3.9
168 18 setosa Sepal_Width 3.5
169 19 setosa Sepal_Width 3.8
170 20 setosa Sepal_Width 3.8
171 21 setosa Sepal_Width 3.4
172 22 setosa Sepal_Width 3.7
173 23 setosa Sepal_Width 3.6
174 24 setosa Sepal_Width 3.3
175 25 setosa Sepal_Width 3.4
176 26 setosa Sepal_Width 3.0
177 27 setosa Sepal_Width 3.4
178 28 setosa Sepal_Width 3.5
179 29 setosa Sepal_Width 3.4
180 30 setosa Sepal_Width 3.2
181 31 setosa Sepal_Width 3.1
182 32 setosa Sepal_Width 3.4
183 33 setosa Sepal_Width 4.1
184 34 setosa Sepal_Width 4.2
185 35 setosa Sepal_Width 3.1
186 36 setosa Sepal_Width 3.2
187 37 setosa Sepal_Width 3.5
188 38 setosa Sepal_Width 3.6
189 39 setosa Sepal_Width 3.0
190 40 setosa Sepal_Width 3.4
191 41 setosa Sepal_Width 3.5
192 42 setosa Sepal_Width 2.3
193 43 setosa Sepal_Width 3.2
194 44 setosa Sepal_Width 3.5
195 45 setosa Sepal_Width 3.8
196 46 setosa Sepal_Width 3.0
197 47 setosa Sepal_Width 3.8
198 48 setosa Sepal_Width 3.2
199 49 setosa Sepal_Width 3.7
200 50 setosa Sepal_Width 3.3
201 51 versicolor Sepal_Width 3.2
202 52 versicolor Sepal_Width 3.2
203 53 versicolor Sepal_Width 3.1
204 54 versicolor Sepal_Width 2.3
205 55 versicolor Sepal_Width 2.8
206 56 versicolor Sepal_Width 2.8
207 57 versicolor Sepal_Width 3.3
208 58 versicolor Sepal_Width 2.4
209 59 versicolor Sepal_Width 2.9
210 60 versicolor Sepal_Width 2.7
211 61 versicolor Sepal_Width 2.0
212 62 versicolor Sepal_Width 3.0
213 63 versicolor Sepal_Width 2.2
214 64 versicolor Sepal_Width 2.9
215 65 versicolor Sepal_Width 2.9
216 66 versicolor Sepal_Width 3.1
217 67 versicolor Sepal_Width 3.0
218 68 versicolor Sepal_Width 2.7
219 69 versicolor Sepal_Width 2.2
220 70 versicolor Sepal_Width 2.5
221 71 versicolor Sepal_Width 3.2
222 72 versicolor Sepal_Width 2.8
223 73 versicolor Sepal_Width 2.5
224 74 versicolor Sepal_Width 2.8
225 75 versicolor Sepal_Width 2.9
226 76 versicolor Sepal_Width 3.0
227 77 versicolor Sepal_Width 2.8
228 78 versicolor Sepal_Width 3.0
229 79 versicolor Sepal_Width 2.9
230 80 versicolor Sepal_Width 2.6
231 81 versicolor Sepal_Width 2.4
232 82 versicolor Sepal_Width 2.4
233 83 versicolor Sepal_Width 2.7
234 84 versicolor Sepal_Width 2.7
235 85 versicolor Sepal_Width 3.0
236 86 versicolor Sepal_Width 3.4
237 87 versicolor Sepal_Width 3.1
238 88 versicolor Sepal_Width 2.3
239 89 versicolor Sepal_Width 3.0
240 90 versicolor Sepal_Width 2.5
241 91 versicolor Sepal_Width 2.6
242 92 versicolor Sepal_Width 3.0
243 93 versicolor Sepal_Width 2.6
244 94 versicolor Sepal_Width 2.3
245 95 versicolor Sepal_Width 2.7
246 96 versicolor Sepal_Width 3.0
247 97 versicolor Sepal_Width 2.9
248 98 versicolor Sepal_Width 2.9
249 99 versicolor Sepal_Width 2.5
250 100 versicolor Sepal_Width 2.8
[ reached 'max' / getOption("max.print") -- omitted 350 rows ]
โท unite ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๊ฐ์ ์ด์ ํ๋๋ก ํฉ์ณ ์๋ก์ด ์ด์ ๋ง๋ค ์ ์๋ค. ์ฒซ ๋ฒ์งธ ์ธ์๋ ์๋ก ๋ง๋ค์ด์ง ์ด์ ์ด๋ฆ์, ๋ ๋ฒ์งธ ์ธ์๋ฅผ ํตํด ํฉ์น๊ณ ์ ํ๋ ์ด์ ์ ํํ๊ณ , sep ์ธ์๋ฅผ ํตํด ํฉ์น ๋, ๋ ์ด์ ์ฌ์ด์ ๋ค์ด๊ฐ๋ ๊ฐ์ ์ ํ ์ ์๋ค.
โท ์์ ์ฝ๋๋ฅผ ํตํด ๊ธฐ์กด ๋ฐ์ดํฐ ํ๋ ์์ separate ํจ์๋ฅผ ํตํด ๋ถ๋ฆฌ๋ ์ด์ ํ๋์ ์ด๋ก ํฉ์น ์ ์๋ค.
'Programming > R' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
CSV ํ์ผ ์ฝ๊ธฐ/์ฐ๊ธฐ (0) | 2020.10.21 |
---|---|
๋ฌธ์์ด ๋ฐ์ดํฐ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ (0) | 2020.10.20 |
์๊ณ์ด ๋ฐ์ดํฐ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ (0) | 2020.10.20 |
apply, sapply, lapply ์ฌ์ฉ๋ฒ (0) | 2020.10.19 |
Leaflet์ ์ด์ฉํ ์ง๋ ์๊ฐํ (0) | 2020.09.12 |