0

我只是在学习如何在 R 中创建自己的 S3 对象。我正在创建的类名为DAT。它包含许多矩阵,这些矩阵将随着我的数据的预处理而被填充。我是这样定义它的:

createDAT <- function(M){                # M is a data.matrix
   # it's assumed that samples are rows and genes are columns already
   z <- list(M_orig <- M,                     # assumed log2 scale
             M_nat <- matrix(),          # M_orig on natural scale
             M_filt <- matrix(),         # after gene filtering
             M_scaled <- matrix(),       # as fraction of all counts
             M_norm <- matrix(),         # after gene normalization
             ZEROGENES <- list(),
             outcome <- list(),
             RefSampleName <- character(),
             RefSample <- matrix(),
             RefSampleUnscaled <- matrix(),
             seed <- numeric())
   #names(z[[2]]) <- "Nat"
   class(z) <- "DAT"
   return(z)
}

我将在此处使用以下代码对其进行实例化:

x <- rnorm(100)
y <- rnorm(100)
df <- data.frame(x, y)
df_wide <- t(df)
data <- createDAT(df_wide)

我有一个“零表达基因”列表,称为ZERO。我要做的就是将该列表添加到DAT的数据实例中。我可以成功地做到这一点:

data[[6]] <- ZERO

但是,为了使事情更直观,而不是引用 data[[6]],我想以某种方式使用 data$ZERO 或其他东西。

有没有办法做到这一点?我一直无法在网上找到任何东西。

谢谢!!

4

1 回答 1

0

您的代码的问题是您<-在函数中使用了箭头(assing 运算符)list。请改用等号,您的 DAT 对象中的元素将被命名。这样,您将能够按$预期使用运算符访问其元素。

于 2019-11-29T00:10:41.563 回答