【问题标题】:How to display a dataframe with factors in R data viewer?如何在 R 数据查看器中显示带有因子的数据框?
【发布时间】:2020-02-05 15:23:45
【问题描述】:

我有一个类,其中包含一个数据框,其中列是因子(来自 phyloseq 包)。看起来是这样的:

> str(a)
'data.frame':   124 obs. of  21 variables:
Formal class 'sample_data' [package "phyloseq"] with 4 slots
  ..@ .Data    :List of 21
  .. ..$ : Factor w/ 2 levels "L25","L53": 2 2 2 2 2 2 2 2 1 1 ...
  .. ..$ : Factor w/ 1 level "feces metagenome": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "none": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "not applicable": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "not applicable": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "feces": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 2 levels "Germany:Berlin",..: 2 2 2 2 2 2 2 2 1 1 ...
  .. ..$ : Factor w/ 1 level "Mus musculus": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "not applicable": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "none": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "none": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 1 level "none": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ : Factor w/ 3 levels "female","male",..: 1 1 1 1 2 2 1 1 3 3 ...

  ..@ names    : chr  "Library" "Organism" "Collection_date" "Env_biome" ...
  ..@ row.names: chr  "285L53" "286L53" "287L53" "288L53" ...
  ..@ .S3Class : chr "data.frame"

我可以毫无问题地在控制台中查看数据,但想在数据查看器中检查它。但是,如果我尝试这样做,我只会得到一个表格,其中显示列名、列类型(即“因子”)以及所述因子的级别,如下所示

Name       Type                               Value
a          List[124x21(phyloseq::sample_data) A data.frame with 124 rows and 21 columns
 Library   factor                             Factor with 2 levels "L25", "L53"
 Organism  factor                             Factor with 1 level: "feces Metagenome"
 etc.

我对 R 有点陌生,并尝试了以下方法:

View(a@.Data)
View(a)
View(a@.Data[])

我怀疑我缺乏 S4 类及其语法方面的知识,你能告诉我如何在数据查看器中显示实际数据吗?

【问题讨论】:

  • 我很确定问题不在于存在因素,而在于它是 S4 的恶作剧。

标签: r rstudio phyloseq


【解决方案1】:

首先尝试将对象更改为数据框,然后应用视图功能

View(as.data.frame(a@.Data))

【讨论】:

  • 好的,成功了!我的列名现在也类似于“structure.c.2L..2L..2L..2L..2L..2L..2L..2L..1L..1L..1L..1L..1L ..” 但我想我可以应付。
  • 太棒了!将参数“col.names = [colnames]”添加到函数中:像 View(as.data.frame(a@.Data), col.names =c("a","b","c")) 会如果您将我的答案标记为解决方案,您会很高兴!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多