dplyr-tidyr-drop_na去除包含空值的行

这篇博客介绍了如何在R语言中利用tidyr包的drop_na函数来过滤数据框中包含空值(NA)的行。示例代码展示了针对特定列col_a和col_b,以及所有列的过滤方法,帮助读者掌握数据预处理的关键步骤。

过滤某列包含空值单元格所在行

df %>% tidyr::drop_na(col_a, col_b)

过滤任意列包含空值单元格所在行

df %>% tidyr::drop_na()
library(GEOquery) library(dplyr) setwd("C:/Users/MSI_NB/Desktop/生信geo数据库/Geo1") GSE57338 <- getGEO('GSE57338', destdir = ".", AnnotGPL = F, getGPL = F) exprSet <- read.table(file = 'GSE57338_series_matrix.txt.gz', sep = '\t', header = T, quote = '""', fill = T, comment.char = "!") GPL11532 <- getGEO("GPL11532", destdir = ".", AnnotGPL = TRUE) GPL11532<-Table(GPL11532) head(GPL11532) library(tidyr) ID2SYMBOL <- GPL11532 %>% #如果平台文件中没用gene symbol 而只有gene assignment 需要从中提取出gene symbol separate( gene_assignment, into = c("prefix", "gene_symbol", "suffix"), sep = " // ", extra = "merge" # 处理多余的分隔符 ) %>% dplyr::select(ID, gene_symbol) %>% #筛选对应ID和genesymbol两列,注意:不同平台文件的id和sample的列名可能不一样 #改列名,注意,这里的ID要跟平台文件的芯片id的列名保持一致,下同 names(exprSet)[1] = "ID" #注释基因名 exprSet <- exprSet %>% inner_join(ID2SYMBOL,by="ID") %>% #合并探针的信息 select(-ID) %>% #去掉多余信息 filter(!is.na(gene_symbol) & gene_symbol != "")%>% #删除GENE_SYMBOL中为空值 separate(gene_symbol,into = c("gene_symbol","drop"),sep = "///")%>% select(-drop) %>% select(gene_symbol, everything()) %>% #重新排列, mutate(rowMean =rowMeans(.[grep("GSM", names(.))])) %>% #求出平均数 arrange(desc(rowMean)) %>% #把表达量的平均值按从大到小排序 distinct(gene_symbol,.keep_all = T) %>% # symbol留下第一个(只留下最大的那个) select(-rowMean) %>% #反向选择去除rowMean这一列 tibble::column_to_rownames(colnames(.)[1]) # 把第一列变成名并删除 #输出表达矩阵 write.csv(exprSet,"exp_GSE57338.csv")
08-20
你代码出错了> # 硅藻群落-环境因子关联分析(优化版) > # 修改日期:2025年10月20日 > # 主要优化:错误处理、日志记录、代码健壮性 > > # ============================== 1. 加载必需包 ============================== > library(ggplot2) > library(readxl) > library(dplyr) > library(vegan) > library(ggrepel) > library(plotrix) > library(ape) > library(psych) > library(corrplot) > library(openxlsx) > library(phyloseq) > library(ggcorrplot) > theme_set(theme_bw()) # 设置默认主题 > > # 安装缺失包的函数(按需启用) > # if (!require("pacman")) install.packages("pacman") > # pacman::p_load(ggplot2, readxl, dplyr, vegan, ggrepel, plotrix, ape, psych, corrplot, openxlsx, phyloseq, ggcorrplot) > > # ============================== 2. 读取硅藻数据 ============================== > # 工作目录 > setwd("D:/p1") > > # 创建输出目录(递归创建,避免报错) > output_dir <- "result/alpha_diatom/env_corr" > if (!dir.exists(output_dir)) dir.create(output_dir, recursive = TRUE) > > # ① 读取硅藻抽平OTU表 > otu <- read.delim( + file = "result/raw/otutab_diatom_rare.txt", + header = TRUE, + sep = "\t", + row.names = 1, + check.names = FALSE + ) > > # ② 读取硅藻分类表 > tax <- read.delim( + file = "result/raw/diatom_otus_tax.txt", + header = FALSE, + sep = "\t", + col.names = c("OTUID", "Taxonomy"), + check.names = FALSE + ) > > # ③ 读取元数据 > factors <- read.delim( + file = "result/metadata.txt", + header = TRUE, + sep = "\t", + row.names = "ID", + check.names = FALSE + ) > > # ============================== 3. 分类表处理(优化版) ============================== > # 使用tax_table构造函数直接处理,避免手动矩阵操作 > # 步骤:拆分Taxonomy列,生成分类矩阵 > tax_split <- strsplit(tax$Taxonomy, ";") > # 确定最大层级数(通常为7,但以防不一致) > max_levels <- max(sapply(tax_split, length)) > tax_levels <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")[1:max_levels] > > # 填充分类矩阵 > tax_mat <- matrix(NA, nrow = nrow(tax), ncol = max_levels, dimnames = list(tax$OTUID, tax_levels)) > for (i in 1:nrow(tax)) { + current_tax <- tax_split[[i]] + if (length(current_tax) > 0) { + # 去除前缀(如"p:") + cleaned_tax <- gsub("^[a-z]:", "", current_tax) + tax_mat[i, 1:length(cleaned_tax)] <- cleaned_tax + } + } > > # 添加OTUID列和原始Taxonomy列,构建数据框 > tax_df <- data.frame( + OTUID = tax$OTUID, + Taxonomy = tax$Taxonomy, + tax_mat, + stringsAsFactors = FALSE + ) > rownames(tax_df) <- tax_df$OTUID > > # 生成NMstr列(非NA分类层级的合并) > tax_df$NMstr <- apply(tax_mat, 1, function(x) paste(na.omit(x), collapse = "|")) > > # ============================== 4. 构建phyloseq对象(强化匹配) ============================== > # 4.1 转换OTU表 > OTU <- otu_table(otu, taxa_are_rows = TRUE) > > # 4.2 转换分类表(只保留分类层级列和NMstr) > tax_columns <- c(tax_levels, "NMstr") > tax_sub <- tax_df[, tax_columns, drop = FALSE] 错误于`[.data.frame`(tax_df, , tax_columns, drop = FALSE): 选择了未定义的列
最新发布
10-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倪桦

有帮助的话请杯咖啡吧,谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值