Making head()s and tail()s of your data with some str()ucture
Time to introduce a few simple, but very useful functions.
head()
- Returns the first few rows of a data frame. By default, 6. To change this, usehead(cash, n = ___)
tail()
- Returns the last few rows of a data frame. By default, 6. To change this, usetail(cash, n = ___)
str()
- Check the structure of an object. This fantastic function will show you the data type of the object you pass in (here, data.frame), and will list each column variable along with its data type.
With a small data set such as yours, head()
and tail()
are not incredibly useful, but imagine if you had a data frame of hundreds or thousands of rows!
This is a part of the course
“Introduction to R for Finance”
Exercise instructions
- Call
head()
oncash
to see the first 4 rows. - Call
tail()
oncash
to see the last 3 rows. - Call
str()
oncash
to check out the structure of your data frame. (You might notice that the class ofcompany
is aFactor
and not acharacter
. Do not fear! This will be covered in Chapter 4. For now, don't worry about it.)
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Call head() for the first 4 rows
# Call tail() for the last 3 rows
# Call str()