str()
Published Oct 7, 2022
Contribute to Docs
The str()
function displays the internal structure of an object such as an array, list, matrix, factor, or data frame.
Syntax
str(object)
The object
parameter is passed to the str()
function to return a string representation that can be combined with other strings.
Example
The following example uses a CSV file named canines.csv
with the following information in it:
First Name | Last Name | Age | Breed |
---|---|---|---|
Max | Lancaster | 18 | Australian Sheperd |
Bella | Stone | 4 | Yorkshire Terrier |
Luna | Bae | 2 | Golden Retriever |
Abe | Thompson | 10 | American Pit Bull Terrier |
Teddy | Keagan | 6 | English Bull Dog |
The following example converts the data into a data frame and then uses the str()
function on it:
# Read filedf <- read.csv("canines.csv")# Display internal structurestr(df)
This returns the following:
'data.frame': 5 obs. of 4 variables:$ First.Name: chr "Max" "Bella" "Luna" "Abe" ...$ Last.Name : chr "Lancaster" "Stone" "Bae" "Thompson" ...$ Age : int 18 4 2 10 6$ Breed : chr "Australian Sheperd" "Yorkshire Terrier""Golden Retriever" "American Pit Bull Terrier" ...
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn R on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn R
Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.Beginner Friendly14 hours