Skip to content

Data Science Tutorials

For Data Science Learners

  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • best books about data analytics
    Best Books to Learn Statistics for Data Science Course
  • Split a Vector into Chunks in R R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • bootstrapping-in-r
    Bootstrapping in R R
  • How do confidence intervals work
    How do confidence intervals work? R
  • Top Data Science Examples You Should Know 2023
    Top Data Science Applications You Should Know 2023 Machine Learning
  • rejection region in hypothesis testing
    Rejection Region in Hypothesis Testing Statistics

Display the structure in R

Posted on May 27May 19 By Admin No Comments on Display the structure in R

Display the structure in R, we will demonstrate how to use the str() function in R to print the structure of a data object.

We will cover three examples: displaying the structure of a data frame, a list, and a vector.

Example 1: Displaying the Structure of a Data Frame

To begin, we need to create an example data frame in R:

data <- data.frame(x1 = 1:5, x2 = letters[1:5], x3 = factor(c("yes", "no", "yes", "yes", "no")))

Next, we can apply the str() function to this data frame:

str(data)

The output will show the structure of the data frame, including the number of observations, the number of variables, and the type of each variable. For example:

'data.frame':    5 obs. of  3 variables:
 $ x1: int  1 2 3 4 5
 $ x2: chr  "a" "b" "c" "d" ...
 $ x3: Factor w/ 2 levels "no","yes": 2 1 2 2 1

This output shows that our data frame has 5 observations and 3 variables.

The variables x1 and x2 are numeric and character, respectively, while x3 is a factor with two levels.

Example 2: Displaying the Structure of a List

Next, we will create an example list and apply the str() function to it:

my_list <- list(letters[3:1], 555, c(1, 3, 5))

The output will show the structure of the list, including the number of elements and the type of each element. For example:

How to create a ggalluvial plot in R? ยป Data Science Tutorials

List of 3
 $ : chr [1:3] "c" "b" "a"
 $ : num 555
 $ : num [1:3] 1 3 5

This output shows that our list has three elements.

The first element is a character vector, the second element is a numeric value, and the third element is a numeric vector.

Example 3: Displaying the Structure of a Vector

Finally, we will create an example vector and apply the str() function to it:

vec <- c(21, 5, 3, 17, 8, 7, 7, 13)

The output will show the structure of the vector, including its type and length. For example:

 num [1:8] 21 5 3 17 8 7 7 13

This output shows that our vector is numeric and has a length of 8.

Conclusion

In this article, we have demonstrated how to use the str() function in R to print the structure of a data object.

We have covered three examples: displaying the structure of a data frame, a list, and a vector.

By using the str() function, you can quickly and easily evaluate the structure of your data objects in R.

  • Random Number Generator in R
  • Select Unique Rows in a Data Frame in R
  • How to Calculate Partial Correlation coefficient in R-Quick Guide
  • Kruskal Wallis test in R-One-way ANOVA Alternative
  • Import CSV Files into R Step-by-Step Guide
  • Cluster Analysis in R
  • How to Calculate Mean Absolute Error in R
  • Conditional Mean in R with examples
R

Post navigation

Previous Post: Convert characters to time in R
Next Post: Replace first match in R

Related Posts

  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
  • Apply central limit throem in r
    Apply Central Limit Theorem in R R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • Mastering the tapply() Function in R R
  • Group By Maximum in R
    Group By Maximum in R R
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • Extract values from vector in R: dplyr R
  • Tips for Data Scientist Interview Openings
    Tips for Data Scientist Interview Openings Course
  • Box Cox transformation in R
    Box Cox transformation in R R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R
  • Mastering the table() Function in R R
  • computational gastronomy for data science
    Computational Gastronomy for Data Science Machine Learning

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme