[英]Rename column names with gsub and sub
I have been reading about renaming column names using gsub and sub.我一直在阅读有关使用 gsub 和 sub 重命名列名的信息。 I want to take various column names 20220427_209944540823_SC835404_12.RCC and truncate to SC835404_12.
我想取各种列名 20220427_209944540823_SC835404_12.RCC 并截断为 SC835404_12。 What am I missing in my code.
我的代码中缺少什么。 Currently, I am getting 209944540823_SC835404_12
目前,我得到 209944540823_SC835404_12
colnames(expr_counts) <- gsub(c(".RCC"), "", sub("^[^_]*_", "", colnames(expr_counts)))
You can use您可以使用
colnames(expr_counts) <- sub(".*_(.*_\\d+)\\.RCC$", "\\1", colnames(expr_counts))
See the regex demo .请参阅正则表达式演示。
Details :详情:
.*
- any zero or more chars as many as possible .*
- 尽可能多的任何零个或多个字符_
- an underscore _
- 下划线(.*_\d+)
- Group 1: any zero or more chars as many as possible, _
, one or more digits (.*_\d+)
- 第 1 组:任何零个或多个字符尽可能多, _
,一个或多个数字\.RCC
- an .RCC
string \.RCC
- 一个.RCC
字符串$
- end of string. $
- 字符串结束。 The replacement is the \1
backreference that replaces the whole match with the Group 1 value.替换是
\1
反向引用,它将整个匹配替换为 Group 1 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.