【问题标题】:R: Error!! : object of type 'S4' is not subsettable回复:错误!! : 'S4' 类型的对象不是子集
【发布时间】:2018-11-01 04:34:48
【问题描述】:

我正在使用 R 的“rehh”包。

我从包的 data2haplohh 函数创建了一个 haplohh 类的对象 chr21。

现在当我尝试将其写入文件时:

 write.table(chr21, file = "CHR21", append = FALSE, quote = TRUE,sep = "\t", eol="\n", na= "NA", dec=".", row.names=TRUE, col.names=TRUE)

我得到的错误是:

as.data.frame.default(x[[i]], optional = TRUE) 中的错误: 不能将类“结构(“haplohh”,包=“rehh”)”强制为data.frame

当我尝试打印 chr21 的前 10 行时,

head(chr21, n=10)

我收到此错误:

x[seq_len(n)] 中的错误:“S4”类型的对象不是子集

好的,现在添加 str(chr21) 的输出:

str(chr21)

正式类 'haplohh' [package "rehh"] 有 6 个插槽

..@ haplo : num [1:10, 1:1010554] 0 2 2 2 0 2 0 2 0 2 ...

..@ 位置:数字 [1:1010554] 9411410 9411645 9411785 9412503 9413228 ...

..@ snp.name: chr [1:1010554] "rs78200054" "rs71235074" "rs71235075" "rs71220884" ...

..@ chr.name: chr "21"

..@ nhap : int 10

..@ nsnp : int 1010554

我是 R 的新手,如果我能知道哪里出错以及如何解决这个错误,那就太好了。

提前致谢!

【问题讨论】:

  • 请提供chr21 对象。在不知道输入对象的情况下很难猜出问题所在。还要考虑write.csv 只接受data.frames 对象或对data.frame 的强制对象。
  • 可能有特定的方法可以到达对象的不同插槽
  • 您能否将以下命令 str(chr21) 的输出添加到您的问题中。这将有助于确定您收到此错误的原因。
  • @Katia:请立即检查问题
  • @nicola :我在问题中添加了一些信息。请检查

标签: r


【解决方案1】:
library(rehh)

#Copy example files in the current working directory.
make.example.files()

#Chreate some sampel data 
chr12<-data2haplohh(hap_file="bta12_hapguess_switch.out",map_file="map.inp",
                  min_maf=0.05,popsel=7,chr.name=12,recode.allele=TRUE)

# Look at the structure of the object (in your case it is called chr21)
str(chr12)
Formal class 'haplohh' [package "rehh"] with 6 slots
..@ haplo   : num [1:280, 1:1202] 2 2 1 2 2 2 1 2 2 2 ...
..@ position: num [1:1202] 79823 125974 175087 219152 256896 ...
..@ snp.name: chr [1:1202] "F1200140" "F1200150" "F1200170" "F1200180" ...
..@ chr.name: chr "12"
..@ nhap    : int 280
..@ nsnp    : int 1202

您可以从此对象中提取各种组件:

# Extract data matrix from it
haplo.matrix <- chr12@haplo

# Extract position
pos <- chr12@position
head(pos)
#[1]  79823 125974 175087 219152 256896 316254

如果您需要将数据恢复为数据框格式,您可以执行以下操作:

df <- data.frame(chr=chr12@chr.name, snp.name=chr12@snp.name, position=chr12@position, stringsAsFactors=FALSE)
df <- cbind(df, t( chr12@haplo))

完成后,您可以使用 head() 和其他常规 R 函数。 但是,如果您需要应用 rehh 包中的功能,则应使用原始 chr21 对象

【讨论】:

    猜你喜欢
    • 2022-09-28
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多