【问题标题】:Remove attributes from dataframe从数据框中删除属性
【发布时间】:2018-09-27 08:49:16
【问题描述】:

我有以下数据框(从 phyloseq 包中的 tax_table 对象转换而来)。

我怎样才能删除属性?

 str(DT2_mat)
'data.frame':   5120 obs. of  7 variables:
 $ : Factor w/ 2 levels "Archaea","Bacteria": 2 2 2 2 2 2 2 2 2 2 ...
  ..- attr(*, "names")= chr  "P11_16513" "P193_8942" "P187_9526" "P11_4543" ...
 $ : Factor w/ 28 levels "Acidobacteria",..: 2 2 2 2 2 2 2 2 2 2 ...
  ..- attr(*, "names")= chr  "P11_16513" "P193_8942" "P187_9526" "P11_4543" ...
 $ : Factor w/ 60 levels "Acidimicrobiia",..: 3 3 3 3 3 3 3 3 3 3 ...
  ..- attr(*, "names")= chr  "P11_16513" "P193_8942" "P187_9526" "P11_4543" ...
 $ : Factor w/ 108 levels "Acholeplasmatales",..: 29 29 29 29 29 29 29 29 29 29 ...
  ..- attr(*, "names")= chr  "P11_16513" "P193_8942" "P187_9526" "P11_4543" ...
 $ : Factor w/ 216 levels "0319-6A21","0319-6G20",..: 58 58 58 58 58 58 58 58 58 58 ...
  ..- attr(*, "names")= chr  "P11_16513" "P193_8942" "P187_9526" "P11_4543" ...
 $ : Factor w/ 699 levels "Abiotrophia",..: 173 173 173 173 173 173 173 173 173 173 ...
  ..- attr(*, "names")= chr  "P11_16513" "P193_8942" "P187_9526" "P11_4543" ...
 $ : Factor w/ 4964 levels "Abiotrophia defectiva Score:0.87",..: 1613 1529 1449 1448 1565 1438 1563 1532 1623 1605 ...
  ..- attr(*, "names")= chr  "P11_16513" "P193_8942" "P187_9526" "P11_4543" ...
P

【问题讨论】:

  • dput( head(DT2_mat) )分享一些数据

标签: r phyloseq


【解决方案1】:

通常,您可以使用attr 函数通过指定要删除的属性并将其设置为NULL 来删除属性。

假设你得到以下信息:

> str(my_df)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   107 obs. of  3 variables:
 $ case_no      : chr  "stuff" "more stuff" "other stuff" "residual stuff" ...
 $ region       : chr  "01" "02" "03" "04" ...
 $ petition     : chr  "RC" "RD" "RM" "RC" ...
 - attr(*, "label")= chr "NLRB7799"

您可以使用attr(my_df, "label") <- NULL删除标签

并通过使用attr(my_df, "class") <- "data.frame" 指定您想要的类来摆脱不需要的额外类

这对我来说效果很好。从 SAS 或 Stata 等其他软件导入数据时,属性会多次标记。 我不喜欢它们,因为它们在合并或绑定到其他数据帧时给我带来了麻烦。希望其他人会发现此方法有用。

【讨论】:

    【解决方案2】:

    其实降低关卡会移除所有属性。

    > str(droplevels.data.frame(DT2_mat))
    'data.frame':   5120 obs. of  7 variables:
     $ : Factor w/ 2 levels "Archaea","Bacteria": 2 2 2 2 2 2 2 2 2 2 ...
     $ : Factor w/ 28 levels "Acidobacteria",..: 2 2 2 2 2 2 2 2 2 2 ...
     $ : Factor w/ 60 levels "Acidimicrobiia",..: 3 3 3 3 3 3 3 3 3 3 ...
     $ : Factor w/ 108 levels "Acholeplasmatales",..: 29 29 29 29 29 29 29 29 29 29 ...
     $ : Factor w/ 216 levels "0319-6A21","0319-6G20",..: 58 58 58 58 58 58 58 58 58 58 ...
     $ : Factor w/ 699 levels "Abiotrophia",..: 173 173 173 173 173 173 173 173 173 173 ...
     $ : Factor w/ 4964 levels "Abiotrophia defectiva Score:0.87",..: 1613 1529 1449 1448 1565 1438 1563 1532 1623 1605 ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 2019-07-08
      • 2017-12-10
      • 2012-05-12
      • 2014-12-19
      • 1970-01-01
      相关资源
      最近更新 更多