To create a cross-tabulation (a two-way table), tidyverse uses count() together with multiple variables:
library(dplyr)
songs %>%
count(THEME, YEAR)
THEME YEAR n
1 Heartbreak 1931 1
2 Heartbreak 1951 1
3 Heartbreak 1958 2
4 Heartbreak 1959 1
5 Heartbreak 1961 2
6 Heartbreak 1962 2
7 Heartbreak 1963 2
8 Heartbreak 1964 6
9 Heartbreak 1965 4
10 Heartbreak 1966 9
11 Heartbreak 1967 3
12 Heartbreak 1968 4
13 Heartbreak 1969 4
14 Heartbreak 1970 5
15 Heartbreak 1971 5
16 Heartbreak 1972 5
17 Heartbreak 1973 3
18 Heartbreak 1974 2
19 Heartbreak 1975 5
20 Heartbreak 1976 1
21 Heartbreak 1977 2
22 Heartbreak 1978 5
23 Heartbreak 1979 4
24 Heartbreak 1980 3
25 Heartbreak 1981 2
26 Heartbreak 1982 1
27 Heartbreak 1983 3
28 Heartbreak 1984 4
29 Heartbreak 1985 1
30 Heartbreak 1986 5
31 Heartbreak 1987 2
32 Heartbreak 1988 2
33 Heartbreak 1989 1
34 Heartbreak 1990 2
35 Heartbreak 1991 1
36 Heartbreak 1992 1
37 Heartbreak 1993 1
38 Heartbreak 1994 2
39 Heartbreak 1995 3
40 Heartbreak 1996 2
41 Heartbreak 1997 3
42 Heartbreak 1998 3
43 Heartbreak 1999 2
44 Heartbreak 2000 2
45 Heartbreak 2001 1
46 Heartbreak 2002 1
47 Heartbreak 2003 4
48 Heartbreak 2004 4
49 Heartbreak 2005 3
50 Heartbreak 2006 2
51 Heartbreak 2007 3
52 Heartbreak 2008 3
53 Life_and_death 1928 2
54 Life_and_death 1935 1
55 Life_and_death 1941 1
56 Life_and_death 1949 1
57 Life_and_death 1950 1
58 Life_and_death 1957 1
59 Life_and_death 1958 2
60 Life_and_death 1959 3
61 Life_and_death 1961 2
62 Life_and_death 1962 1
63 Life_and_death 1963 2
64 Life_and_death 1964 2
65 Life_and_death 1965 4
66 Life_and_death 1966 5
67 Life_and_death 1967 2
68 Life_and_death 1968 4
69 Life_and_death 1969 4
70 Life_and_death 1970 4
71 Life_and_death 1971 5
72 Life_and_death 1972 6
73 Life_and_death 1973 5
74 Life_and_death 1974 5
75 Life_and_death 1975 1
76 Life_and_death 1976 1
77 Life_and_death 1977 2
78 Life_and_death 1978 2
79 Life_and_death 1979 2
80 Life_and_death 1980 3
81 Life_and_death 1981 1
82 Life_and_death 1982 3
83 Life_and_death 1983 1
84 Life_and_death 1984 2
85 Life_and_death 1985 1
86 Life_and_death 1986 1
87 Life_and_death 1987 2
88 Life_and_death 1988 3
89 Life_and_death 1989 2
90 Life_and_death 1990 2
91 Life_and_death 1991 4
92 Life_and_death 1992 2
93 Life_and_death 1993 2
94 Life_and_death 1994 8
95 Life_and_death 1996 2
96 Life_and_death 1997 3
97 Life_and_death 2000 4
98 Life_and_death 2002 1
99 Life_and_death 2003 2
100 Life_and_death 2004 1
101 Life_and_death 2005 3
102 Life_and_death 2006 3
103 Life_and_death 2007 2
104 Life_and_death 2008 2
105 Love 1928 1
106 Love 1939 1
107 Love 1952 1
108 Love 1954 1
109 Love 1955 1
110 Love 1956 5
111 Love 1958 1
112 Love 1959 2
113 Love 1960 1
114 Love 1961 4
115 Love 1962 2
116 Love 1963 5
117 Love 1964 11
118 Love 1965 4
119 Love 1966 11
120 Love 1967 6
121 Love 1968 7
122 Love 1969 2
123 Love 1970 3
124 Love 1971 3
125 Love 1972 6
126 Love 1973 8
127 Love 1974 6
128 Love 1975 3
129 Love 1976 1
130 Love 1977 5
131 Love 1978 4
132 Love 1979 3
133 Love 1982 2
134 Love 1984 1
135 Love 1985 2
136 Love 1986 2
137 Love 1987 2
138 Love 1988 1
139 Love 1989 2
140 Love 1992 2
141 Love 1993 1
142 Love 1994 3
143 Love 1995 3
144 Love 1996 1
145 Love 2000 2
146 Love 2001 1
147 Love 2003 2
148 Love 2007 3
149 Love 2008 1
150 Party_songs 1938 1
151 Party_songs 1944 1
152 Party_songs 1959 1
153 Party_songs 1962 1
154 Party_songs 1963 1
155 Party_songs 1965 3
156 Party_songs 1967 5
157 Party_songs 1968 5
158 Party_songs 1970 1
159 Party_songs 1972 4
160 Party_songs 1973 2
161 Party_songs 1974 6
162 Party_songs 1975 4
163 Party_songs 1976 9
164 Party_songs 1977 6
165 Party_songs 1978 6
166 Party_songs 1979 17
167 Party_songs 1980 5
168 Party_songs 1981 7
169 Party_songs 1982 5
170 Party_songs 1983 11
171 Party_songs 1984 7
172 Party_songs 1985 2
173 Party_songs 1986 1
174 Party_songs 1987 4
175 Party_songs 1988 1
176 Party_songs 1989 4
177 Party_songs 1990 2
178 Party_songs 1991 1
179 Party_songs 1992 2
180 Party_songs 1993 1
181 Party_songs 1994 1
182 Party_songs 1995 3
183 Party_songs 1997 1
184 Party_songs 1998 1
185 Party_songs 1999 6
186 Party_songs 2000 1
187 Party_songs 2001 3
188 Party_songs 2002 4
189 Party_songs 2003 2
190 Party_songs 2004 2
191 Party_songs 2005 3
192 Party_songs 2006 1
193 Party_songs 2007 2
194 Party_songs 2008 6
195 People_and_places 1916 1
196 People_and_places 1922 1
197 People_and_places 1928 2
198 People_and_places 1931 1
199 People_and_places 1932 1
200 People_and_places 1936 1
201 People_and_places 1939 1
202 People_and_places 1940 1
203 People_and_places 1941 1
204 People_and_places 1946 2
205 People_and_places 1950 1
206 People_and_places 1951 2
207 People_and_places 1953 1
208 People_and_places 1954 2
209 People_and_places 1955 1
210 People_and_places 1956 4
211 People_and_places 1958 1
212 People_and_places 1959 3
213 People_and_places 1960 3
214 People_and_places 1961 5
215 People_and_places 1962 1
216 People_and_places 1963 3
217 People_and_places 1964 3
218 People_and_places 1965 8
219 People_and_places 1966 7
220 People_and_places 1967 9
221 People_and_places 1968 8
222 People_and_places 1969 5
223 People_and_places 1971 6
224 People_and_places 1972 2
225 People_and_places 1973 2
226 People_and_places 1974 4
227 People_and_places 1975 4
228 People_and_places 1976 1
229 People_and_places 1977 4
230 People_and_places 1978 2
231 People_and_places 1979 3
232 People_and_places 1980 5
233 People_and_places 1981 2
234 People_and_places 1983 5
235 People_and_places 1984 2
236 People_and_places 1985 3
237 People_and_places 1986 2
238 People_and_places 1987 2
239 People_and_places 1988 1
240 People_and_places 1989 1
241 People_and_places 1991 1
242 People_and_places 1993 2
243 People_and_places 1995 1
244 People_and_places 1998 1
245 People_and_places 1999 1
246 People_and_places 2001 1
247 People_and_places 2002 1
248 People_and_places 2005 3
249 People_and_places 2006 2
250 People_and_places 2007 1
251 People_and_places 2008 1
252 Politics_and_protest 1929 2
253 Politics_and_protest 1932 1
254 Politics_and_protest 1938 1
255 Politics_and_protest 1939 1
256 Politics_and_protest 1944 1
257 Politics_and_protest 1960 1
258 Politics_and_protest 1963 3
259 Politics_and_protest 1964 5
260 Politics_and_protest 1965 6
261 Politics_and_protest 1966 1
262 Politics_and_protest 1967 3
263 Politics_and_protest 1968 6
264 Politics_and_protest 1969 8
265 Politics_and_protest 1970 8
266 Politics_and_protest 1971 7
267 Politics_and_protest 1973 2
268 Politics_and_protest 1974 1
269 Politics_and_protest 1975 2
270 Politics_and_protest 1976 4
271 Politics_and_protest 1977 6
272 Politics_and_protest 1978 2
273 Politics_and_protest 1979 4
274 Politics_and_protest 1980 6
275 Politics_and_protest 1981 2
276 Politics_and_protest 1982 4
277 Politics_and_protest 1983 1
278 Politics_and_protest 1984 4
279 Politics_and_protest 1985 7
280 Politics_and_protest 1986 2
281 Politics_and_protest 1987 2
282 Politics_and_protest 1988 4
283 Politics_and_protest 1989 5
284 Politics_and_protest 1990 1
285 Politics_and_protest 1991 1
286 Politics_and_protest 1992 4
287 Politics_and_protest 1993 1
288 Politics_and_protest 1996 1
289 Politics_and_protest 1999 1
290 Politics_and_protest 2000 1
291 Politics_and_protest 2002 2
292 Politics_and_protest 2003 1
293 Politics_and_protest 2004 2
294 Politics_and_protest 2005 3
295 Politics_and_protest 2006 3
296 Politics_and_protest 2007 4
297 Politics_and_protest 2008 4
298 Sex 1928 1
299 Sex 1954 1
300 Sex 1955 1
301 Sex 1957 2
302 Sex 1961 1
303 Sex 1963 1
304 Sex 1965 4
305 Sex 1966 4
306 Sex 1967 5
307 Sex 1968 5
308 Sex 1969 8
309 Sex 1970 2
310 Sex 1971 4
311 Sex 1972 4
312 Sex 1973 2
313 Sex 1974 3
314 Sex 1975 4
315 Sex 1976 1
316 Sex 1977 2
317 Sex 1978 4
318 Sex 1979 1
319 Sex 1980 3
320 Sex 1981 5
321 Sex 1982 3
322 Sex 1983 4
323 Sex 1984 2
324 Sex 1985 2
325 Sex 1986 4
326 Sex 1987 4
327 Sex 1988 3
328 Sex 1989 4
329 Sex 1990 3
330 Sex 1991 2
331 Sex 1992 3
332 Sex 1993 3
333 Sex 1994 4
334 Sex 1995 2
335 Sex 1996 1
336 Sex 1997 1
337 Sex 1998 2
338 Sex 1999 1
339 Sex 2000 2
340 Sex 2001 1
341 Sex 2002 3
342 Sex 2003 3
343 Sex 2006 3
344 Sex 2007 1
345 Sex 2008 2
346 <NA> NA 6
This produces a long-format contingency table, which is often easier to manipulate than the wide matrix returned by table().
If you want the joint proportions:
songs %>%
count(THEME, YEAR) %>%
mutate(prop = n / sum(n))
THEME YEAR n prop
1 Heartbreak 1931 1 0.001
2 Heartbreak 1951 1 0.001
3 Heartbreak 1958 2 0.002
4 Heartbreak 1959 1 0.001
5 Heartbreak 1961 2 0.002
6 Heartbreak 1962 2 0.002
7 Heartbreak 1963 2 0.002
8 Heartbreak 1964 6 0.006
9 Heartbreak 1965 4 0.004
10 Heartbreak 1966 9 0.009
11 Heartbreak 1967 3 0.003
12 Heartbreak 1968 4 0.004
13 Heartbreak 1969 4 0.004
14 Heartbreak 1970 5 0.005
15 Heartbreak 1971 5 0.005
16 Heartbreak 1972 5 0.005
17 Heartbreak 1973 3 0.003
18 Heartbreak 1974 2 0.002
19 Heartbreak 1975 5 0.005
20 Heartbreak 1976 1 0.001
21 Heartbreak 1977 2 0.002
22 Heartbreak 1978 5 0.005
23 Heartbreak 1979 4 0.004
24 Heartbreak 1980 3 0.003
25 Heartbreak 1981 2 0.002
26 Heartbreak 1982 1 0.001
27 Heartbreak 1983 3 0.003
28 Heartbreak 1984 4 0.004
29 Heartbreak 1985 1 0.001
30 Heartbreak 1986 5 0.005
31 Heartbreak 1987 2 0.002
32 Heartbreak 1988 2 0.002
33 Heartbreak 1989 1 0.001
34 Heartbreak 1990 2 0.002
35 Heartbreak 1991 1 0.001
36 Heartbreak 1992 1 0.001
37 Heartbreak 1993 1 0.001
38 Heartbreak 1994 2 0.002
39 Heartbreak 1995 3 0.003
40 Heartbreak 1996 2 0.002
41 Heartbreak 1997 3 0.003
42 Heartbreak 1998 3 0.003
43 Heartbreak 1999 2 0.002
44 Heartbreak 2000 2 0.002
45 Heartbreak 2001 1 0.001
46 Heartbreak 2002 1 0.001
47 Heartbreak 2003 4 0.004
48 Heartbreak 2004 4 0.004
49 Heartbreak 2005 3 0.003
50 Heartbreak 2006 2 0.002
51 Heartbreak 2007 3 0.003
52 Heartbreak 2008 3 0.003
53 Life_and_death 1928 2 0.002
54 Life_and_death 1935 1 0.001
55 Life_and_death 1941 1 0.001
56 Life_and_death 1949 1 0.001
57 Life_and_death 1950 1 0.001
58 Life_and_death 1957 1 0.001
59 Life_and_death 1958 2 0.002
60 Life_and_death 1959 3 0.003
61 Life_and_death 1961 2 0.002
62 Life_and_death 1962 1 0.001
63 Life_and_death 1963 2 0.002
64 Life_and_death 1964 2 0.002
65 Life_and_death 1965 4 0.004
66 Life_and_death 1966 5 0.005
67 Life_and_death 1967 2 0.002
68 Life_and_death 1968 4 0.004
69 Life_and_death 1969 4 0.004
70 Life_and_death 1970 4 0.004
71 Life_and_death 1971 5 0.005
72 Life_and_death 1972 6 0.006
73 Life_and_death 1973 5 0.005
74 Life_and_death 1974 5 0.005
75 Life_and_death 1975 1 0.001
76 Life_and_death 1976 1 0.001
77 Life_and_death 1977 2 0.002
78 Life_and_death 1978 2 0.002
79 Life_and_death 1979 2 0.002
80 Life_and_death 1980 3 0.003
81 Life_and_death 1981 1 0.001
82 Life_and_death 1982 3 0.003
83 Life_and_death 1983 1 0.001
84 Life_and_death 1984 2 0.002
85 Life_and_death 1985 1 0.001
86 Life_and_death 1986 1 0.001
87 Life_and_death 1987 2 0.002
88 Life_and_death 1988 3 0.003
89 Life_and_death 1989 2 0.002
90 Life_and_death 1990 2 0.002
91 Life_and_death 1991 4 0.004
92 Life_and_death 1992 2 0.002
93 Life_and_death 1993 2 0.002
94 Life_and_death 1994 8 0.008
95 Life_and_death 1996 2 0.002
96 Life_and_death 1997 3 0.003
97 Life_and_death 2000 4 0.004
98 Life_and_death 2002 1 0.001
99 Life_and_death 2003 2 0.002
100 Life_and_death 2004 1 0.001
101 Life_and_death 2005 3 0.003
102 Life_and_death 2006 3 0.003
103 Life_and_death 2007 2 0.002
104 Life_and_death 2008 2 0.002
105 Love 1928 1 0.001
106 Love 1939 1 0.001
107 Love 1952 1 0.001
108 Love 1954 1 0.001
109 Love 1955 1 0.001
110 Love 1956 5 0.005
111 Love 1958 1 0.001
112 Love 1959 2 0.002
113 Love 1960 1 0.001
114 Love 1961 4 0.004
115 Love 1962 2 0.002
116 Love 1963 5 0.005
117 Love 1964 11 0.011
118 Love 1965 4 0.004
119 Love 1966 11 0.011
120 Love 1967 6 0.006
121 Love 1968 7 0.007
122 Love 1969 2 0.002
123 Love 1970 3 0.003
124 Love 1971 3 0.003
125 Love 1972 6 0.006
126 Love 1973 8 0.008
127 Love 1974 6 0.006
128 Love 1975 3 0.003
129 Love 1976 1 0.001
130 Love 1977 5 0.005
131 Love 1978 4 0.004
132 Love 1979 3 0.003
133 Love 1982 2 0.002
134 Love 1984 1 0.001
135 Love 1985 2 0.002
136 Love 1986 2 0.002
137 Love 1987 2 0.002
138 Love 1988 1 0.001
139 Love 1989 2 0.002
140 Love 1992 2 0.002
141 Love 1993 1 0.001
142 Love 1994 3 0.003
143 Love 1995 3 0.003
144 Love 1996 1 0.001
145 Love 2000 2 0.002
146 Love 2001 1 0.001
147 Love 2003 2 0.002
148 Love 2007 3 0.003
149 Love 2008 1 0.001
150 Party_songs 1938 1 0.001
151 Party_songs 1944 1 0.001
152 Party_songs 1959 1 0.001
153 Party_songs 1962 1 0.001
154 Party_songs 1963 1 0.001
155 Party_songs 1965 3 0.003
156 Party_songs 1967 5 0.005
157 Party_songs 1968 5 0.005
158 Party_songs 1970 1 0.001
159 Party_songs 1972 4 0.004
160 Party_songs 1973 2 0.002
161 Party_songs 1974 6 0.006
162 Party_songs 1975 4 0.004
163 Party_songs 1976 9 0.009
164 Party_songs 1977 6 0.006
165 Party_songs 1978 6 0.006
166 Party_songs 1979 17 0.017
167 Party_songs 1980 5 0.005
168 Party_songs 1981 7 0.007
169 Party_songs 1982 5 0.005
170 Party_songs 1983 11 0.011
171 Party_songs 1984 7 0.007
172 Party_songs 1985 2 0.002
173 Party_songs 1986 1 0.001
174 Party_songs 1987 4 0.004
175 Party_songs 1988 1 0.001
176 Party_songs 1989 4 0.004
177 Party_songs 1990 2 0.002
178 Party_songs 1991 1 0.001
179 Party_songs 1992 2 0.002
180 Party_songs 1993 1 0.001
181 Party_songs 1994 1 0.001
182 Party_songs 1995 3 0.003
183 Party_songs 1997 1 0.001
184 Party_songs 1998 1 0.001
185 Party_songs 1999 6 0.006
186 Party_songs 2000 1 0.001
187 Party_songs 2001 3 0.003
188 Party_songs 2002 4 0.004
189 Party_songs 2003 2 0.002
190 Party_songs 2004 2 0.002
191 Party_songs 2005 3 0.003
192 Party_songs 2006 1 0.001
193 Party_songs 2007 2 0.002
194 Party_songs 2008 6 0.006
195 People_and_places 1916 1 0.001
196 People_and_places 1922 1 0.001
197 People_and_places 1928 2 0.002
198 People_and_places 1931 1 0.001
199 People_and_places 1932 1 0.001
200 People_and_places 1936 1 0.001
201 People_and_places 1939 1 0.001
202 People_and_places 1940 1 0.001
203 People_and_places 1941 1 0.001
204 People_and_places 1946 2 0.002
205 People_and_places 1950 1 0.001
206 People_and_places 1951 2 0.002
207 People_and_places 1953 1 0.001
208 People_and_places 1954 2 0.002
209 People_and_places 1955 1 0.001
210 People_and_places 1956 4 0.004
211 People_and_places 1958 1 0.001
212 People_and_places 1959 3 0.003
213 People_and_places 1960 3 0.003
214 People_and_places 1961 5 0.005
215 People_and_places 1962 1 0.001
216 People_and_places 1963 3 0.003
217 People_and_places 1964 3 0.003
218 People_and_places 1965 8 0.008
219 People_and_places 1966 7 0.007
220 People_and_places 1967 9 0.009
221 People_and_places 1968 8 0.008
222 People_and_places 1969 5 0.005
223 People_and_places 1971 6 0.006
224 People_and_places 1972 2 0.002
225 People_and_places 1973 2 0.002
226 People_and_places 1974 4 0.004
227 People_and_places 1975 4 0.004
228 People_and_places 1976 1 0.001
229 People_and_places 1977 4 0.004
230 People_and_places 1978 2 0.002
231 People_and_places 1979 3 0.003
232 People_and_places 1980 5 0.005
233 People_and_places 1981 2 0.002
234 People_and_places 1983 5 0.005
235 People_and_places 1984 2 0.002
236 People_and_places 1985 3 0.003
237 People_and_places 1986 2 0.002
238 People_and_places 1987 2 0.002
239 People_and_places 1988 1 0.001
240 People_and_places 1989 1 0.001
241 People_and_places 1991 1 0.001
242 People_and_places 1993 2 0.002
243 People_and_places 1995 1 0.001
244 People_and_places 1998 1 0.001
245 People_and_places 1999 1 0.001
246 People_and_places 2001 1 0.001
247 People_and_places 2002 1 0.001
248 People_and_places 2005 3 0.003
249 People_and_places 2006 2 0.002
250 People_and_places 2007 1 0.001
251 People_and_places 2008 1 0.001
252 Politics_and_protest 1929 2 0.002
253 Politics_and_protest 1932 1 0.001
254 Politics_and_protest 1938 1 0.001
255 Politics_and_protest 1939 1 0.001
256 Politics_and_protest 1944 1 0.001
257 Politics_and_protest 1960 1 0.001
258 Politics_and_protest 1963 3 0.003
259 Politics_and_protest 1964 5 0.005
260 Politics_and_protest 1965 6 0.006
261 Politics_and_protest 1966 1 0.001
262 Politics_and_protest 1967 3 0.003
263 Politics_and_protest 1968 6 0.006
264 Politics_and_protest 1969 8 0.008
265 Politics_and_protest 1970 8 0.008
266 Politics_and_protest 1971 7 0.007
267 Politics_and_protest 1973 2 0.002
268 Politics_and_protest 1974 1 0.001
269 Politics_and_protest 1975 2 0.002
270 Politics_and_protest 1976 4 0.004
271 Politics_and_protest 1977 6 0.006
272 Politics_and_protest 1978 2 0.002
273 Politics_and_protest 1979 4 0.004
274 Politics_and_protest 1980 6 0.006
275 Politics_and_protest 1981 2 0.002
276 Politics_and_protest 1982 4 0.004
277 Politics_and_protest 1983 1 0.001
278 Politics_and_protest 1984 4 0.004
279 Politics_and_protest 1985 7 0.007
280 Politics_and_protest 1986 2 0.002
281 Politics_and_protest 1987 2 0.002
282 Politics_and_protest 1988 4 0.004
283 Politics_and_protest 1989 5 0.005
284 Politics_and_protest 1990 1 0.001
285 Politics_and_protest 1991 1 0.001
286 Politics_and_protest 1992 4 0.004
287 Politics_and_protest 1993 1 0.001
288 Politics_and_protest 1996 1 0.001
289 Politics_and_protest 1999 1 0.001
290 Politics_and_protest 2000 1 0.001
291 Politics_and_protest 2002 2 0.002
292 Politics_and_protest 2003 1 0.001
293 Politics_and_protest 2004 2 0.002
294 Politics_and_protest 2005 3 0.003
295 Politics_and_protest 2006 3 0.003
296 Politics_and_protest 2007 4 0.004
297 Politics_and_protest 2008 4 0.004
298 Sex 1928 1 0.001
299 Sex 1954 1 0.001
300 Sex 1955 1 0.001
301 Sex 1957 2 0.002
302 Sex 1961 1 0.001
303 Sex 1963 1 0.001
304 Sex 1965 4 0.004
305 Sex 1966 4 0.004
306 Sex 1967 5 0.005
307 Sex 1968 5 0.005
308 Sex 1969 8 0.008
309 Sex 1970 2 0.002
310 Sex 1971 4 0.004
311 Sex 1972 4 0.004
312 Sex 1973 2 0.002
313 Sex 1974 3 0.003
314 Sex 1975 4 0.004
315 Sex 1976 1 0.001
316 Sex 1977 2 0.002
317 Sex 1978 4 0.004
318 Sex 1979 1 0.001
319 Sex 1980 3 0.003
320 Sex 1981 5 0.005
321 Sex 1982 3 0.003
322 Sex 1983 4 0.004
323 Sex 1984 2 0.002
324 Sex 1985 2 0.002
325 Sex 1986 4 0.004
326 Sex 1987 4 0.004
327 Sex 1988 3 0.003
328 Sex 1989 4 0.004
329 Sex 1990 3 0.003
330 Sex 1991 2 0.002
331 Sex 1992 3 0.003
332 Sex 1993 3 0.003
333 Sex 1994 4 0.004
334 Sex 1995 2 0.002
335 Sex 1996 1 0.001
336 Sex 1997 1 0.001
337 Sex 1998 2 0.002
338 Sex 1999 1 0.001
339 Sex 2000 2 0.002
340 Sex 2001 1 0.001
341 Sex 2002 3 0.003
342 Sex 2003 3 0.003
343 Sex 2006 3 0.003
344 Sex 2007 1 0.001
345 Sex 2008 2 0.002
346 <NA> NA 6 0.006
If you want conditional proportions, for example, proportions within each theme, you can again use group_by():
songs %>%
count(THEME, YEAR) %>%
group_by(THEME) %>%
mutate(prop_within_theme = n / sum(n)) %>%
ungroup()
# A tibble: 346 × 4
THEME YEAR n prop_within_theme
<chr> <int> <int> <dbl>
1 Heartbreak 1931 1 0.00690
2 Heartbreak 1951 1 0.00690
3 Heartbreak 1958 2 0.0138
4 Heartbreak 1959 1 0.00690
5 Heartbreak 1961 2 0.0138
6 Heartbreak 1962 2 0.0138
7 Heartbreak 1963 2 0.0138
8 Heartbreak 1964 6 0.0414
9 Heartbreak 1965 4 0.0276
10 Heartbreak 1966 9 0.0621
# ℹ 336 more rows
This is the tidyverse equivalent of:
prop.table(table(sex, education), 1) (row proportions)
prop.table(table(sex, education), 2) (column proportions)
but expressed through clear piping grammar.
If needed, you can pivot from long format to wide format:
library(tidyr)
songs %>%
count(THEME, YEAR) %>%
tidyr::pivot_wider(names_from = YEAR, values_from = n, values_fill = 0)
# A tibble: 8 × 76
THEME `1931` `1951` `1958` `1959` `1961` `1962` `1963` `1964` `1965` `1966`
<chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 Heartbr… 1 1 2 1 2 2 2 6 4 9
2 Life_an… 0 0 2 3 2 1 2 2 4 5
3 Love 0 0 1 2 4 2 5 11 4 11
4 Party_s… 0 0 0 1 0 1 1 0 3 0
5 People_… 1 2 1 3 5 1 3 3 8 7
6 Politic… 0 0 0 0 0 0 3 5 6 1
7 Sex 0 0 0 0 1 0 1 0 4 4
8 <NA> 0 0 0 0 0 0 0 0 0 0
# ℹ 65 more variables: `1967` <int>, `1968` <int>, `1969` <int>, `1970` <int>,
# `1971` <int>, `1972` <int>, `1973` <int>, `1974` <int>, `1975` <int>,
# `1976` <int>, `1977` <int>, `1978` <int>, `1979` <int>, `1980` <int>,
# `1981` <int>, `1982` <int>, `1983` <int>, `1984` <int>, `1985` <int>,
# `1986` <int>, `1987` <int>, `1988` <int>, `1989` <int>, `1990` <int>,
# `1991` <int>, `1992` <int>, `1993` <int>, `1994` <int>, `1995` <int>,
# `1996` <int>, `1997` <int>, `1998` <int>, `1999` <int>, `2000` <int>, …
This produces the classic contingency matrix. In the next section we will talk more about pivoting data.
Back to top