Count The Records In A Table

Now that we can access the data in our table, we can (finally!) perform calculations and analyze our data. The first piece of data that we may want to know is quite simply how many records are we working with. In our relatively simple table, for example, you might want to know how many complete tragedies we have from antiquity and how many were written by each author. If you enter the command nrow(trag.length) in the R environment, you will see the result 31. Likewise, the command nrow(frank.words) gives the result 2,020 and the command nrow(odyssey.monsters), reports 22,385.

It is also possible to count the number of records in a subset of your data using with the nrow() command by specifying a subset of the data table within the parentheses. The command nrow(trag.length[trag.length$Author == "Aeschylus", ]), for example, returns the output of 7 while nrow(odyssey.monsters[odyssey.monsters$Segment=="Aeolus", ]) returns the output of 760.

While the number of rows in a table is an extremely simple metric, it does provide a model of the syntax of most R commands. In R, a command like nrows is followed by parentheses. The name of the variable or vector of numbers that will be processed using the command is place inside the parentheses. ((As we get to more complex commands, we will also see that some R commands allow you to enter other options separated by commas after the vector inside the parentheses.)) If the command is entered in the interactive mode, the results are displayed on the screen. The results of any command can also be placed within a variable with the <- and -> operators. For example, the commands nrow(trag.length) -> rows and rows <- nrow(trag.length) will both assign the number of rows in our sample table to the variable rows

<<-- Previous: Using Data Frames
Next: Calculate Totals and Subtotals-->>