in `edesign[, dummy.cols]`: ! Can't subset columns with `dummy.cols`. ✖ Subscrip...
🚨 错误信息
gene_matrix <- read_delim("geneMatrix_normalized_log2cpm.txt", delim = "\t", col_names = TRUE)
Rows: 13692 Columns: 37
── Column specification ──────────────────────────────────────────────────────────────────────────────
Delimiter: "\t"
chr (1): symble
dbl (36): 24hC1, 5DC1, 6hC1, 72hC1, 24hC2, 5DC2, 6hC2, 72hC2, 24hC3, 5DC3, 6hC3, 72hC3, 24hH1, 5DH...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
> meta_data <- read_csv("meta_all.csv")
Rows: 36 Columns: 6
── Column specification ──────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (1): sample
dbl (5): time, Ctrl, Low, High, replicate
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
>
> # 设置行名为基因名
> rownames(gene_matrix) <- gene_matrix$symble
> gene_matrix <- gene_matrix[, -1] # 去掉第一列的基因名
>
> # 转换数据类型
> meta_data$time <- as.numeric(meta_data$time)
> meta_data$Ctrl <- as.numeric(meta_data$Ctrl)
> meta_data$Low <- as.numeric(meta_data$Low)
> meta_data$High <- as.numeric(meta_data$High)
> meta_data$replicate <- as.numeric(meta_data$replicate)
>
> # 创建一个新的列 treatment
> meta_data$treatment <- NA
> meta_data$treatment[meta_data$Ctrl == 1] <- 0
> meta_data$treatment[meta_data$Low == 1] <- 1
> meta_data$treatment[meta_data$High == 1] <- 2
>
> # 设置行名为样本名
> rownames(meta_data) <- meta_data$sample
>
> # 构建设计矩阵
> design <- make.design.matrix(meta_data, degree = 3, time.col = 2, repl.col = 6, group.cols = "treatment")
Error in `edesign[, dummy.cols]`:
! Can't subset columns with `dummy.cols`.
✖ Subscript `dummy.cols` can't contain missing values.
✖ It has a missing value at location 1.
Run `rlang::last_trace()` to see where the error occurred.
🤖 AI解决方案
Error in `edesign[, dummy.cols]`:
! Can't subset columns with `dummy.cols`.
✖ Subscript `dummy.cols` can't contain missing values.
✖ It has a missing value at location 1.
原因分析
make.design.matrix 函数在构建设计矩阵时,要求 time.col 和 repl.col 指定的列(即 time 和 replicate)不能包含缺失值(NA)。错误信息指出 dummy.cols(内部用于标识需要创建虚拟变量的列)在位置 1 存在缺失值,这直接关联到 time.col 或 repl.col 指向的列中存在 NA。
解决方案
在调用 make.design.matrix 之前,确保 meta_data 中用于 time.col 和 repl.col 的列(即 time 和 replicate)不包含任何 NA 值。
直接可用的代码:
# 检查并移除 meta_data 中 time 和 replicate 列的 NA 值
meta_data <- meta_data[!is.na(meta_data$time) & !is.na(meta_data$replicate), ]
# 重新构建设计矩阵
design <- make.design.matrix(meta_data, degree = 3, time.col = 2, repl.col = 6, group.cols = "treatment")
预防措施
- 数据加载后校验:在加载
meta_data.csv文件后,立即检查time和replicate列是否存在 NA 值,并进行处理。 - 源文件检查:在导入数据前,检查
meta_all.csv文件本身,确保time和replicate列没有遗漏或错误录入的空值。