When creating a table for frequencies, I get different displays between console output and RStudio viewer. Important to note, the information remains the same across, it's just the display that's different.
Generate Data
id <- rep(1:20, each = 200)
colors <- sample(c("red",
"blue",
"green",
"yellow",
"orange",
"brown", "
white",
"pink"),
size = 4000, replace = TRUE)
df <- as.data.frame(cbind(id, colors))
tally <- with(df, table(id, colors))
> tally
colors
id \n white blue brown green orange pink red yellow
1 33 22 33 25 21 23 24 19
10 16 17 34 30 25 24 33 21
11 30 27 28 28 22 18 26 21
12 25 24 21 25 21 35 23 26
13 22 28 22 30 21 28 29 20
14 25 24 21 28 30 23 24 25
15 26 20 20 20 35 32 26 21
16 32 21 14 33 28 14 25 33
17 29 24 31 23 21 28 14 30
18 28 30 22 22 20 23 26 29
19 22 30 25 22 27 34 16 24
2 21 30 19 18 37 23 32 20
20 27 26 24 36 25 16 19 27
3 25 27 24 26 20 27 22 29
4 26 23 28 25 22 28 25 23
5 30 26 16 28 21 31 25 23
6 31 30 21 26 20 25 24 23
7 30 23 25 28 28 24 21 21
8 31 18 37 14 28 25 26 21
9 22 22 23 32 22 21 28 30
However, when using View()
I get the data displayed in a different format
View(tally)
Is there a way to make the Viewer show the data in the same format as in the console (where columns correspond to the colors)?
EDIT 25-AUG-2019
From @camille's comments below, I've learned that this question has been asked before here. The point conveyed is that RStudio's viewer knows how to display data frames properly but not tables. Therefore, the solution provided in the old post would suggest to use either the generic as.data.frame()
or the more specific as.data.frame.table()
/as.data.frame.matrix()
to convert the table to a data frame, and RStudio's viewer should subsequently be able to render the data properly as in the original table's layout.
Unfortunately, neither of the proposed functions works for me. Also evident from this comment that the solution provided in that post might have ceased from working, and it's not just my case. The question and solution posted in 2012, so perhaps something has changed between then and now?
For the sake of demonstrating the behavior I'm talking about:
tally <- with(df, table(id, colors))
is.table(tally)
[1] TRUE
> tally
colors
id \n white blue brown green orange pink red yellow
1 33 22 33 25 21 23 24 19
10 16 17 34 30 25 24 33 21
11 30 27 28 28 22 18 26 21
12 25 24 21 25 21 35 23 26
13 22 28 22 30 21 28 29 20
14 25 24 21 28 30 23 24 25
15 26 20 20 20 35 32 26 21
16 32 21 14 33 28 14 25 33
17 29 24 31 23 21 28 14 30
18 28 30 22 22 20 23 26 29
19 22 30 25 22 27 34 16 24
2 21 30 19 18 37 23 32 20
20 27 26 24 36 25 16 19 27
3 25 27 24 26 20 27 22 29
4 26 23 28 25 22 28 25 23
5 30 26 16 28 21 31 25 23
6 31 30 21 26 20 25 24 23
7 30 23 25 28 28 24 21 21
8 31 18 37 14 28 25 26 21
9 22 22 23 32 22 21 28 30
> tally <- as.data.frame.table(tally)
> is.table(tally)
[1] FALSE
> is.data.frame(tally)
[1] TRUE
> tally
id colors Freq
1 1 \n white 33
2 10 \n white 16
3 11 \n white 30
4 12 \n white 25
5 13 \n white 22
6 14 \n white 25
7 15 \n white 26
8 16 \n white 32
9 17 \n white 29
10 18 \n white 28
11 19 \n white 22
12 2 \n white 21
13 20 \n white 27
14 3 \n white 25
15 4 \n white 26
16 5 \n white 30
17 6 \n white 31
18 7 \n white 30
19 8 \n white 31
20 9 \n white 22
21 1 blue 22
22 10 blue 17
23 11 blue 27
24 12 blue 24
25 13 blue 28
26 14 blue 24
27 15 blue 20
28 16 blue 21
29 17 blue 24
30 18 blue 30
31 19 blue 30
32 2 blue 30
33 20 blue 26
34 3 blue 27
35 4 blue 23
36 5 blue 26
37 6 blue 30
38 7 blue 23
39 8 blue 18
40 9 blue 22
41 1 brown 33
42 10 brown 34
43 11 brown 28
44 12 brown 21
45 13 brown 22
46 14 brown 21
47 15 brown 20
48 16 brown 14
49 17 brown 31
50 18 brown 22
51 19 brown 25
52 2 brown 19
53 20 brown 24
54 3 brown 24
55 4 brown 28
56 5 brown 16
57 6 brown 21
58 7 brown 25
59 8 brown 37
60 9 brown 23
61 1 green 25
62 10 green 30
63 11 green 28
64 12 green 25
65 13 green 30
66 14 green 28
67 15 green 20
68 16 green 33
69 17 green 23
70 18 green 22
71 19 green 22
72 2 green 18
73 20 green 36
74 3 green 26
75 4 green 25
76 5 green 28
77 6 green 26
78 7 green 28
79 8 green 14
80 9 green 32
81 1 orange 21
82 10 orange 25
83 11 orange 22
84 12 orange 21
85 13 orange 21
86 14 orange 30
87 15 orange 35
88 16 orange 28
89 17 orange 21
90 18 orange 20
91 19 orange 27
92 2 orange 37
93 20 orange 25
94 3 orange 20
95 4 orange 22
96 5 orange 21
97 6 orange 20
98 7 orange 28
99 8 orange 28
100 9 orange 22
101 1 pink 23
102 10 pink 24
103 11 pink 18
104 12 pink 35
105 13 pink 28
106 14 pink 23
107 15 pink 32
108 16 pink 14
109 17 pink 28
110 18 pink 23
111 19 pink 34
112 2 pink 23
113 20 pink 16
114 3 pink 27
115 4 pink 28
116 5 pink 31
117 6 pink 25
118 7 pink 24
119 8 pink 25
120 9 pink 21
121 1 red 24
122 10 red 33
123 11 red 26
124 12 red 23
125 13 red 29
126 14 red 24
127 15 red 26
128 16 red 25
129 17 red 14
130 18 red 26
131 19 red 16
132 2 red 32
133 20 red 19
134 3 red 22
135 4 red 25
136 5 red 25
137 6 red 24
138 7 red 21
139 8 red 26
140 9 red 28
141 1 yellow 19
142 10 yellow 21
143 11 yellow 21
144 12 yellow 26
145 13 yellow 20
146 14 yellow 25
147 15 yellow 21
148 16 yellow 33
149 17 yellow 30
150 18 yellow 29
151 19 yellow 24
152 2 yellow 20
153 20 yellow 27
154 3 yellow 29
155 4 yellow 23
156 5 yellow 23
157 6 yellow 23
158 7 yellow 21
159 8 yellow 21
160 9 yellow 30
As can be seen above, when I use as.data.frame.table
, not only does it not solve my problem, but it makes the unwanted layout of data to now appear in console as well.
Any ideas how to convert to a data frame while preserving the format I had when originally running with(df, table(id, colors))
?
Thanks!