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()
This exercise is part of the course
Introduction to R for Finance
Learn essential data structures such as lists and data frames and apply that knowledge directly to financial examples.
Arguably the most important data structure in R, the data frame is what most of your data will take the form of. It combines the structure of a matrix with the flexibility of having different types of data in each column.
Exercise 1: What is a data frame?Exercise 2: Create your first data.frame()Exercise 3: What goes in a data frame?Exercise 4: Making head()s and tail()s of your data with some str()uctureExercise 5: Naming your columns / rowsExercise 6: Data frame manipulationExercise 7: Accessing and subsetting data frames (1)Exercise 8: Accessing and subsetting data frames (2)Exercise 9: Accessing and subsetting data frames (3)Exercise 10: Adding new columnsExercise 11: Present valueExercise 12: Present value of projected cash flows (1)Exercise 13: Present value of projected cash flows (2)What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.