【发布时间】:2019-08-05 23:34:11
【问题描述】:
我必须处理多个数据帧才能执行同一组操作。因此,我将它们插入到一个列表中,并通过使用 lapply 执行某种操作。 然后,我想通过使用 for 循环将每个数据帧分别保存为 .txt 文件。这是我写的代码:
all <- lapply(names(sampleList),function(mysample){
aux <- read.table(sampleList[[mysample]], col.names=c("Chromosome","Position","Ref_allele","Alt_allele","Fraction","Fw_ref","Rv_ref","Fw_alt","Rv_alt"))
aux <- mutate(aux, ID=paste0(Chromosome, ":", Position)) %>% distinct(ID, .keep_all=T)
})
for( i in 1:length(all))
write.table(all[i], paste0(all[i],"_filtered.txt"))
我希望有 n 个新数据帧,命名为原始数据帧 + _filtered 最后。但这就是实际发生的情况:
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'list(Chromosome = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 [... truncated]
但这不是我的数据框包含的内容。通过 View(as.data.frame(all[n])) 我看到我的数据框看起来很正常。
非常感谢您的帮助。
【问题讨论】:
-
write.table的第二个参数应该是文件名,而是paste0(all[i],...),它是data.frame(嵌入长度为 1 的list),而不是文件名.也许你可以用Map(write.table, all, paste0(names(all), "_filtered.txt"))完全替换你的for循环。 -
(这样写让我很痛苦......我建议您不要将变量命名为与基本 R 函数相同:
all和any很常见,以至于它其他人可能有点难以阅读您的代码,更难解决问题(尤其是如果您不理解错误object of type 'closure' is not subsettable)。)
标签: r