R str() function - View object structure
R str() function is used to quickly view the internal structure of R objects.
str() is a powerful tool for data structure exploration, showing object type, number of elements, and content preview.
The syntax of the str() function is as follows:
str(object)
Parameter description:
objectThe R object to view.
Example
# View vector structure
x <- c(1, 2, 3, 4, 5)
print("Vector structure:")
str(x)
# View 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
)
print("Data frame structure:")
str(df)
# View list structure
my_list <- list(
name = "Zhang San",
age = 25,
scores = c(88, 92, 76)
)
print("List structure:")
str(my_list)
x <- c(1, 2, 3, 4, 5)
print("Vector structure:")
str(x)
# View 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
)
print("Data frame structure:")
str(df)
# View list structure
my_list <- list(
name = "Zhang San",
age = 25,
scores = c(88, 92, 76)
)
print("List structure:")
str(my_list)
The output of the above code 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 76Other extensions
R language examples