R str() Function - Inspecting Object Structure
The R str() function is used to quickly inspect the internal structure of an R object.
str() is a powerful tool for exploring data structures, showing the object type, number of elements, and a preview of the content.
The syntax of the str() function is as follows:
str(object)
Parameter Description:
-
object The R object to inspect.
Example
Inspect vector structure
x
<-
c
(
1
,
2
,
3
,
4
,
5
)
(
"Vector structure:"
)
str
(
x
)
Inspect data frame structure
df
<-
data.frame
(
Name
=
c
(
"Zhang San"
,
"Li Si"
,
"Wang Wu"
)
,
Age
=
c
(
25
,
30
,
28
)
,
Score
=
c
(
88.5
,
92.0
,
76.5
)
,
stringsAsFactors
=
FALSE
)
(
"Data frame structure:"
)
str
(
df
)
Inspect list structure
my_list
<-
list
(
name
=
"Zhang San"
,
age
=
25
,
scores
=
c
(
88
,
92
,
76
)
)
(
"List structure:"
)
str
(
my_list
)
The output of the code above is:
[1] "向量结构:"
num [1:5] 1 2 3 4 5
[1] "数据框结构:"
'data.frame': 3 obs. of 3 variables:
$ 姓名: chr "张三" "李四" "王五"
$ 年龄: num 25 30 28
$ 成绩: num 88.5 92 76.5
[1] "列表结构:"
List of 3
$ name : chr "张三"
$ age : num 25
$ scores: num [1:3] 88 92 76
R Language Examples