Chapter 12. Useful Tricks

Introduction

The recipes in this chapter are neither obscure numerical calculations nor deep statistical techniques. Yet they are useful functions and idioms that you will likely need at one time or another.

12.1. Peeking at Your Data

Problem

You have a lot of data—too much to display at once. Nonetheless, you want to see some of the data.

Solution

Use head to view the first few data or rows:

> head(x)

Use tail to view the last few data or rows:

> tail(x)

Discussion

Printing a large dataset is pointless because everything just rolls off your screen. Use head to see a little bit of the data:

> head(dfrm)
           x           y          z
1  0.7533110  0.57562846 -0.1710760
2  2.0143547  0.83312274  0.3698584
3 -0.3551345  0.57471542  2.0132348
4  2.0281678  0.78945319 -0.5378854
5 -2.2168745  0.01758024  1.8344879
6  0.7583962 -1.78214755  2.2848990

Use tail to see the last few rows and the number of rows. Here, we see that this data frame has 10,120 rows:

> tail(dfrm)
               x           y          z
10115 -0.0314354 -0.74988291 -0.2048963
10116 -0.4779001  0.93407510  1.0509977
10117 -1.1314402  1.89308417  1.7207972
10118  0.4891881 -1.20792811 -1.4630227
10119  1.2349013 -0.09615198 -0.9887513
10120 -1.3763834 -2.25309628  0.9296106

See Also

See Recipe 12.15 for seeing the structure of your variable’s contents.

12.2. Widen Your Output

Problem

You are printing wide datasets. R is wrapping the output, making it hard to read.

Solution

Set the width option to reflect the true number of columns in your output window:

> options(width=numcols)

Discussion ...

Get R Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.