r - 如何在 R 的循环中创建名称基于文件名的数据框?
问题描述
我正在尝试创建一个函数,该函数循环遍历我目录中的文件,创建多个名称基于文件名的数据帧,并将新创建的数据帧写入 .csv。但是,我创建的解决方案仅适用于目录中的最后一个文件,而不适用于所有文件。谁能指出我的错误?
##Peak annotation function for all the files in the directory
##Loading required libraries
library(GenomicFeatures)
library(GenomicRanges)
library(AnnotationDbi)
library(ChIPseeker)
##Making some annotations
hg38_TxDB <- makeTxDbFromGFF("/Users/dari_obukhova/data/Homo_sapiens/UCSC/hg38/Annotation/Genes/genes.gtf")
saveDb(hg38_TxDB, file = "/Users/dari_obukhova/data/Homo_sapiens/UCSC/hg38/Annotation/TxDB.sqlite")
hg38_Annotation <- loadDb("/Users/dari_obukhova/data/Homo_sapiens/UCSC/hg38/Annotation/TxDB.sqlite")
##Function to use for all the files in the directory
all_peaks_files <- list.files(pattern = ".xls", full.names = T)
for (file in all_peaks_files) {
peak_list <- list()
peaks_df <- read.delim(fileName, comment.char = "#")
peaks_gr <- makeGRangesFromDataFrame(peaks_df, keep.extra.columns = T)
annotated_peaks_df <- as.data.frame(annotatePeak(peaks_gr, TxDb = hg38_Annotation))
peak_list[[(length(peak_list) + 1)]] <- annotated_peaks_df
}
peak_list
但是,返回的列表只有一个元素(all_peaks_files 中的列表文件)。此外,我不确定如何根据每个特定文件名重命名我创建的每个 df。我将不胜感激任何建议!<3
解决方案
大家,谢谢你的好建议。我能够将我的代码修改为有效的代码。这是我修改后的代码。
##Function to annotate some files with peaks generated by CHIPSeq
##Loading necessary libraries
library(GenomicFeatures)
library(GenomicRanges)
library(AnnotationDbi)
library(ChIPseeker)
##Making some annotations of a used genome
hESCpeaks_df <- read.delim("H3K27ac_hESC_peaks.xls", comment.char = "#")
hg38_TxDB <- makeTxDbFromGFF("/Users/dari_obukhova/data/Homo_sapiens/UCSC/hg38/Annotation/Genes/genes.gtf")
saveDb(hg38_TxDB, file = "/Users/dari_obukhova/data/Homo_sapiens/UCSC/hg38/Annotation/TxDB.sqlite")
hg38_Annotation <- loadDb("/Users/dari_obukhova/data/Homo_sapiens/UCSC/hg38/Annotation/TxDB.sqlite")
##Annotating all files in the directory
all_peaks_files <- list.files(pattern = ".xls" ) ##choosing all the files with .xls extension from the working directory
peak_list <- list() ##Empty list for files to be added in
for (file in all_peaks_files) {
peaks_df <- read.delim(file, comment.char = "#")
peaks_gr <- makeGRangesFromDataFrame(assign(file, peaks_df), keep.extra.columns = T)
annotated_peaks_df <- as.data.frame(annotatePeak(peaks_gr, TxDb = hg38_Annotation))
peak_list[[(length(peak_list) + 1)]] <- assign(file, annotated_peaks_df)
}
peak_list
推荐阅读
- reinforcement-learning - PPO 算法异常
- flutter - 如何制作一个像杯子/碗一样飘动的形状?
- javascript - 由于溢出:隐藏,滑块边框在表格中不可见
- android - 如何设置间隔以从加速度计注册数据 - 运动传感器
- c# - 更改 int 变量值时遇到问题(井字游戏)
- maven - 当前的 Maven 配置文件启动服务器并保持,以便下一个 Maven 配置文件不会被执行。有什么解决办法吗?
- python - 如何将 Numpy 2D 数组的选定元素乘以 1D 数组的相应元素?
- rust - 错误[E0277]:期望一个 `std::ops::FnOnce<(&select::node::Node<'_>,)>` 闭包,找到 `select::predicate::Class<&&str>`
- php - Gravity Forms 提交表单数据,然后重定向到自定义表单操作 URL?
- c# - 将数据值保存到同一类 ASP.NET MVC 中的多个表中