如何在R中使用特定模式重命名文件? [英] How to rename files with a specific pattern in R?

查看:127
本文介绍了如何在R中使用特定模式重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些 .fcs data.000X 的a>文件(其中X = 1、2、3 ...).

There are some .fcs files in a data.000X format (where X = 1, 2, 3...) in a directory.

我想将每个 n 文件重命名为以下格式: exp.fcs (其中 exp 是来自矢量的文本),如果要重命名的文件是 .fcs 文件.

I want to rename every n file to the following format: exp.fcs (where exp is a text from a vector) if the file to be renamed is an .fcs file.

换句话说:我想将文件重命名为exp.txt,其中<​​em> exp 是文本,而不是连续的字母,即F,cA,K等等

in other words: I want to rename files to exp.txt, where exp is a text and not a consecutive letter(s) i.e. F, cA, K, etc.

例如,来自:

data.0001, data.0002, data.0003, data.0004, data.0005, data.0006...  

textF_a.fcs, textF_b.fcs, textF_c.fcs, textVv_a.fcs, textVv_b.fcs, textVv_c.fcs ...  

我尝试使用文件来完成此操作.rename(from,to)但失败,因为参数的长度不同(我不知道这是什么意思):

I tried to do it with file.rename(from, to) but failed as the arguments have different lengths (and I don't know what it means):

a <- list.files(path = ".", pattern = "data.*$")  
b <- paste("data", 1:1180, ".fcs", sep = "")  
file.rename(a, b)

推荐答案

根据您的评论,一个问题是您的第一个文件未命名为"data.001",而是命名为"data.1".使用这个:

Based on your comments, one issue is that your first file isn't named "data.001" - it's named "data.1". Use this:

b <- sprintf("data%.4d.fcs", seq(a)) 

它最多为3 0(由于您似乎有1000多个文件,所以可能会更好)来索引< 1000,以便所有名称都具有相同的宽度.如果您确实只想查看"data.001"之类的内容,请在命令中使用%.3d.

It prepends up to 3 0s (since it seems you have 1000+ files, this may be better) to indices < 1000, so that all names have the same width. If you really just want to see things like "data.001", then use %.3d in the command.

这篇关于如何在R中使用特定模式重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆