When trying to display data, the natural format of a basic printout is not always appealing to look at. Data frames may have a printout that resembles a table but often lacks the readability of a table. This is particularly true with larger data frames.
Within R programming it is possible to display a data frame as a table in a couple of different ways. The simplest approach, and one that does not require any packages, is the View function. The View function has the format of View(df) where “df” is the name of the data frame that you are trying to display as a table. It is a simple function that can display your data frame as an attractive-looking table. This display does not show up in the console window but as a tab along with your code.
When you apply the view function to a data frame, it creates a display table that allows you to select a column by which to order the data. In most cases, the data in the table is arranged by alphabetizing words and letters, while placing numbers in numerical order. When you click on the column containing the row names the table is arranged so that the row names are in the order they are found in the data frame.
Here are two examples of the view function in action. The first one is a basic data frame, while the second one has meaningful row and column names.
> df = data.frame(A = c(“A”, “A”,”B”, “C”, “D”, “E”),
+ B = c(“F”, “A”, “E”, “C”, “D”, “B”),
+ C = c( 44, 22, 33, 29, 31, 16))
A B C
1 A F 44
2 A A 22
3 B E 33
4 C C 29
5 D D 31
6 E B 16
In this example, we have a basic data frame. What you see below it, is the standard printout, and not the result of the view function.
> df = data.frame(Name = c(“Bob”, “Tom”, “Bev”, “Tim”, “Sue”, “Bill”, “Rick”),
+ Job = c(“Boss”, “Clerk”, “Sales”, “Sales”, “Feild”, “Feild”, “Feild”),
+ pay = c( 2000, 700, 1000, 1500, 400, 300, 350),
+ row.names = c(“Office 1″,”Office 2”, “Car 1”, “Car 2”, “Truck 1”, “Truck 2”, “Truck 3”))
Name Job pay
Office 1 Bob Boss 2000
Office 2 Tom Clerk 700
Car 1 Bev Sales 1000
Car 2 Tim Sales 1500
Truck 1 Sue Feild 400
Truck 2 Bill Feild 300
Truck 3 Rick Feild 350
In this example, we have a data frame complete with row and column names. While its normal display is similar to that of a table, a table would have more functionality.
One application for the view function would be to create a table of your data that you could add to a presentation, or put online, by doing a screen capture. The second application would be using the added functionality of the table to better understand the data that you are looking at. In either case, the view function improves the readability of the data particularly when it is a large data frame.
The view function is a useful tool for displaying a data frame in an easier-to-read manner. It allows you to change the order of the data so that you can understand it better. It is a handy tool that you do not want to be without.