如何在r中提取字符矢量的特定部分?

发布于 2025-02-04 13:36:31 字数 330 浏览 1 评论 0原文

我有这样的文件名作为字符向量;

filenames <- c('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')

不想在伪造中拥有数字,并且想要数字“/”分离。 因此所需的输出是;

filenames_desired <- c('1194/1220/1479/891','1194/1221/1421/891')

我尝试了gsub,但不知道如何删除伪造中的数字。

提前致谢

I have file names like this as a character vector;

filenames <- c('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')

Don't want to have digits in pharanthesis and want to have digits "/" separated.
So the desired output is;

filenames_desired <- c('1194/1220/1479/891','1194/1221/1421/891')

I tried with gsub but didn't know how to remove digits in pharanthesis.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不喜欢何必死缠烂打 2025-02-11 13:36:31

使用Stringr,环顾(?= - )含义:必须遵循破折号和sapply:

filenames <- c('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')

sapply(stringr::str_extract_all(filenames, "\\d+(?=-)"), 
       paste0, 
       collapse = "/") 

[1] "1194/1220/1479/891" "1194/1221/1421/891"

Using stringr, looking around (?=-) meaning: has to be followed by a dash and sapply:

filenames <- c('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')

sapply(stringr::str_extract_all(filenames, "\\d+(?=-)"), 
       paste0, 
       collapse = "/") 

[1] "1194/1220/1479/891" "1194/1221/1421/891"
舟遥客 2025-02-11 13:36:31

我们可以使用一个sub()在此处致电:

filenames <- c("1194-1220-1479-891--(133.07).RDS",
               "1194-1221-1421-891--(101.51).RDS")

output <- sub("(\\d+)-(\\d+)-(\\d+)-(\\d+).*", "\\1/\\2/\\3/\\4", filenames)
output

[1] "1194/1220/1479/891" "1194/1221/1421/891"

We could use a single sub() call here:

filenames <- c("1194-1220-1479-891--(133.07).RDS",
               "1194-1221-1421-891--(101.51).RDS")

output <- sub("(\\d+)-(\\d+)-(\\d+)-(\\d+).*", "\\1/\\2/\\3/\\4", filenames)
output

[1] "1194/1220/1479/891" "1194/1221/1421/891"
ゞ花落谁相伴 2025-02-11 13:36:31

只需使用gsub\\ - 。*:删除之后的所有内容,包括-

gsub('\\--.*', '', filenames)

[1] "1194-1220-1479-891" "1194-1221-1421-891"

Just use gsub with \\--.*: removes everything after and including --:

gsub('\\--.*', '', filenames)

[1] "1194-1220-1479-891" "1194-1221-1421-891"
人生戏 2025-02-11 13:36:31

如我所见,您的名字的前18个字符是您的最后名称的基础;因此,您可以使用以下代码

# Initial names
  filenames <- ('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')

# Extract the first 18 elements of "filenames"
  nam <- substr(filenames, 1, 18)
# Replace "-" by "/"
  final.names <- str_replace_all(nam, "-", "/")

As I can see, the first 18 characters of your names are the base of your final names; so, you can use the following code

# Initial names
  filenames <- ('1194-1220-1479-891--(133.07).RDS','1194-1221-1421-891--(101.51).RDS')

# Extract the first 18 elements of "filenames"
  nam <- substr(filenames, 1, 18)
# Replace "-" by "/"
  final.names <- str_replace_all(nam, "-", "/")
美煞众生 2025-02-11 13:36:31

您可以使用strsplit从每个列表中提取第一个元素,然后使用gsub

gsub('-', '/', sapply(strsplit(filenames, '--'), `[[`, 1))

哪个将产生

#"1194/1220/1479/891" "1194/1221/1421/891"

You could use strsplit to extract the first element from each list and then use gsub:

gsub('-', '/', sapply(strsplit(filenames, '--'), `[[`, 1))

which will yield

#"1194/1220/1479/891" "1194/1221/1421/891"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文