[英]Rename files in R - delete character
I have multiple files to analyze that are now called "E - 04(fld 1 wv Red - Cy5)" ad I want to delete the 0 so that they become just "E - 4(fld 1 wv Red - Cy5)".我有多个文件要分析,现在称为“E - 04(fld 1 wv Red - Cy5)”广告我想删除0,以便它们变成“E - 4(fld 1 wv Red - Cy5)”。 Is there a way to do it?
有没有办法做到这一点?
Thanks!谢谢!
Just to add to Rui Barradas answer, you could use the package stringr
and remove the zero from the name or names, by typing:只是要添加到 Rui Barradas 的答案中,您可以使用包
stringr
并通过键入以下内容从一个或多个名称中删除零:
fileNames <- "E - 04(fld 1 wv Red - Cy5)"
stringr::str_remove(fileNames, '0')
[1] "E - 4(fld 1 wv Red - Cy5)"
This base R regex seems to do the job.这个基本的 R正则表达式似乎可以完成这项工作。
x <- "E - 04(fld 1 wv Red - Cy5)"
sub("(^[^[:digit:]]*)0([[:digit:]]+.*$)", "\\1\\2", x)
#[1] "E - 4(fld 1 wv Red - Cy5)"
If you want to change many filenames, maybe a function will make it easier to perform the task.如果您想更改许多文件名,也许一个功能可以使执行任务更容易。
changeName <- function(x){
sub("(^[^[:digit:]]*)0([[:digit:]]+.*$)", "\\1\\2", x)
}
x <- "E - 04(fld 1 wv Red - Cy5)"
changeName(x)
#[1] "E - 4(fld 1 wv Red - Cy5)"
y <- c("E - 04(fld 1 wv Red - Cy5)", "E - 06789(fld 1 wv Red - Cy5)")
#[1] "E - 4(fld 1 wv Red - Cy5)" "E - 6789(fld 1 wv Red - Cy5)"
changeName(y)
You can use look arounds and str_remove:您可以使用环顾四周和 str_remove:
> library(stringr)
> s
[1] "E - 04(fld 1 wv Red - Cy5)"
> str_remove(s, '(?<=E - )0(?=4\\(fld 1 wv Red - Cy5\\))')
[1] "E - 4(fld 1 wv Red - Cy5)"
>
We can also use sub
我们也可以使用
sub
sub("0", "", str1)
#[1] "E - 4(fld 1 wv Red - Cy5)"
str1 <- "E - 04(fld 1 wv Red - Cy5)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.