R语言中attr()函数

 

attr()函数允许你创建任意属性并将其与对象关联。

 

1、

> a <- 1:5
> b <- letters[1:5]
> c <- LETTERS[1:5]
> dat <- data.frame(a, b, c)
> dat                              ## 测试数据框
  a b c
1 1 a A
2 2 b B
3 3 c C
4 4 d D
5 5 e E
> attributes(dat)                 ## 查看全部属性
$names
[1] "a" "b" "c"

$class
[1] "data.frame"

$row.names
[1] 1 2 3 4 5

> attr(dat, "tag")                 ## 查看指定属性
NULL
> attr(dat, "tag") <- "this is a test!"      ## 添加新属性tag
> attributes(dat)                            ## 查看全部属性
$names
[1] "a" "b" "c"

$class
[1] "data.frame"

$row.names
[1] 1 2 3 4 5

$tag
[1] "this is a test!"

> attr(dat, "tag")                          ## 查看指定属性
[1] "this is a test!"

 

参考:https://blog.csdn.net/sinat_40586658/article/details/120442829

 

posted @ 2022-05-15 19:46  小鲨鱼2018  阅读(1985)  评论(0编辑  收藏  举报