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.

You may also like...

Leave a Reply

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

19 + 14 =

Ads Blocker Image Powered by Code Help Pro

Quality articles need supporters. Will you be one?

You currently have an Ad Blocker on.

Please support FINNSTATS.COM by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock