R Language: str Function

0

The str function is a very useful function in R that can be used to compactly display the internal structure of an R object. It can be used to look at functions, vectors, data frames, matrices, and lists. The str function can give you a quick overview of what an object is, what it contains, and how it is structured. This can be very helpful when you are trying to understand an R object or when you are debugging code.

The str function takes an object as input and returns a one-line summary of the object. The summary includes the object's class, the number of elements in the object, and the first few elements of the object. For example, the following code would use the str function to display the structure of a vector named `x`:

x <- c(1, 2, 3, 4, 5)

str(x)

The output of the `str(x)` function would be:

## int [1:5] 1 2 3 4 5

This output tells us that `x` is a vector of integers with 5 elements. The first few elements of `x` are 1, 2, 3, 4, and 5.

The str function can also be used to display the structure of more complex objects, such as data frames and matrices. For example, the following code would use the str function to display the structure of the `airquality` data frame:

data(airquality)

str(airquality)

The output of the `str(airquality)` function would be:

## 'data.frame': 153 obs. of 6 variables:

##  $ Ozone : int 41 36 12 18 28 NA 19 8 12 18 ...

##  $ Solar.R : int 190 118 149 194 172 NA 151 121 144 192 ...

##  $ Wind : num 2.5 2.3 1.2 1.9 1.4 NA 2.1 1.7 1.8 2 ...

##  $ Temp : int 72 67 74 77 81 NA 75 69 71 75 ...

##  $ Month : int 5 5 5 5 5 NA 5 5 5 5 ...

##  $ Day : int 1 2 3 4 5 NA 6 7 8 9 ...

This output tells us that the `airquality` data frame has 153 observations and 6 variables. The names of the variables are `Ozone`, `Solar.R`, `Wind`, `Temp`, `Month`, and `Day`. The `str(airquality)` function also displays the first few elements of each variable.

Post a Comment

0Comments
Post a Comment (0)