如何删除所有属性? [英] How to erase all attributes?

查看:60
本文介绍了如何删除所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据中删除所有属性,并应用此解决方案.但是 one_entry()(原始)和我的 one_entry2()都不起作用,我不知道为什么.

I want to erase all attributes from data and applied this solution. However neither one_entry() (the original) nor my one_entry2() will work and I don't see why.

one_entry2 <- function(x) {
  attr(x, "label") <- NULL
  attr(x, "labels") <- NULL
}

> lapply(df1, one_entry2)
$`id`
NULL

$V1
NULL

$V2
NULL

$V3
NULL

我们如何做到这一点?

数据:

df1 <- setNames(data.frame(matrix(1:12, 3, 4)), 
                c("id", paste0("V", 1:3)))
attr(df1$V1, "labels") <- LETTERS[1:4]
attr(df1$V1, "label") <- letters[1:4]
attr(df1$V2, "labels") <- LETTERS[1:4]
attr(df1$V2, "label") <- letters[1:4]
attr(df1$V3, "labels") <- LETTERS[1:4]
attr(df1$V3, "label") <- letters[1:4]

> str(df1)
'data.frame':   3 obs. of  4 variables:
 $ id: int  1 2 3
 $ V1: int  4 5 6
  ..- attr(*, "labels")= chr  "A" "B" "C" "D"
  ..- attr(*, "label")= chr  "a" "b" "c" "d"
 $ V2: int  7 8 9
  ..- attr(*, "labels")= chr  "A" "B" "C" "D"
  ..- attr(*, "label")= chr  "a" "b" "c" "d"
 $ V3: int  10 11 12
  ..- attr(*, "labels")= chr  "A" "B" "C" "D"
  ..- attr(*, "label")= chr  "a" "b" "c" "d"

推荐答案

要删除所有属性,该如何处理

To remove all attributes, how about this

df1[] <- lapply(df1, function(x) { attributes(x) <- NULL; x })
str(df1)
#'data.frame':  3 obs. of  4 variables:
# $ id: int  1 2 3
# $ V1: int  4 5 6
# $ V2: int  7 8 9
# $ V3: int  10 11 12

这篇关于如何删除所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆