微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在R Studio中分割和写入txt文件时出错

如何解决在R Studio中分割和写入txt文件时出错

我有这个大文件名为Objects_Population-AllCells.txt,大约3GB,该文件有25704373行和132个变量。我想读取文件,并根据一个变量(称为处理和的列)拆分行。在本专栏中,我将在不同的条件(3S或UNS)下进行实验性药物治疗,即用“ _”链接的字符串。因此,拆分会将所有经过相同处理的行放在一起。拆分文件后,我要写出拆分文件并使用处理总和给出文件名。

我的代码如下:

#load libraries
library(tidyverse)
library(vroom)
library(dplyr)
library(stringr)

#read in the file,skip the first 9 rows
files<-vroom("Objects_Population - AllCells.txt",delim = "\t",skip = 9,col_names = T)

#split the files based on treatmentsum
splited<- files %>% 
  group_split(files$treatmentsum)

#write out the splitted files
output<- lapply(splited,function(i){
  for (i in 1:length(splited)) {
    write.table(splited[[i]][,1:131],file=paste(unique(splited[[i]]$treatmentsum),".txt"),sep="\t",row.names=FALSE)

  }
 })

因此,当我运行它时,文件可以正确读取,并且拆分工作正常,并且按预期方式分散处理,即得到了1092(在环境中显示)的列表,每个列表都包含具有相同处理的行。但是,它每次向我写入233个文件后,代码都会消失。我已经筛查了错误,并且生成的所有文件均为3S,没有生成UNS文件(如右下方文件目录屏幕快照所示)。有人可以帮助我,让我知道错误的含义吗?

enter image description here

解决方法

由于处理名称,我想出了一些文件名,其中会有“ /”。受此https://stackoverflow.com/a/49647853/12362355

的启发
library(tidyverse)
library(vroom)
library(dplyr)
library(stringr)
files<-vroom("Objects_Population - AllCells.txt",delim = "\t",skip = 9,col_names = T)


splited<- files %>% 
  group_split(files$treatmentsum)



output<- lapply(splited,function(i){
  for (i in 1:length(splited)) {
    write.table(splited[[i]][,1:131],file=paste0(gsub("/","",unique(splited[[i]]$treatmentsum)),".txt"),sep="\t",row.names=FALSE)

  }
 })

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。