R Built-in Data
Built-in data is sample data that ships with R. It sits ready to use in any script.
The first rows of cars
head asks for the first four rows of cars. The output shows the speed and dist columns for those rows.
print(head(cars, 4))Counting the rows
nrow counts the rows in cars. The output shows 50.
print(nrow(cars))Average speed and distance
The first print shows the mean speed in cars. The second print shows the mean distance in cars.
print(mean(cars$speed))
print(mean(cars$dist))Example
print(max(cars$speed))
print(max(cars$dist))Key points
- R ships with sample datasets for practice.
- A built-in dataset needs no loading call.
- The cars dataset holds speed and stopping distance for 50 cars.
- head() shows the first rows of a dataset.
- A short look at the top rows keeps a big table small on screen.
In this example, max() finds the fastest speed and the longest stopping distance in cars. Both values come from columns of the same dataset.
Note
Built-in data suits practice runs and small tests.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท rdrr.io R Snippets
Exercise
What does head(cars, 4) show?