我只是在学习如何在 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 或其他东西。
有没有办法做到这一点?我一直无法在网上找到任何东西。
谢谢!!