colnames(countdata) = gsub("Aligned.sortedByCoord.out.bam_rem.bam", "", colnames(countdata))
colnames(countdata)
dput(colnames(countdata))
c("BAF60A_KD_1_S13", "BAF60A_KD_2_S14", "BAF60A_RA_1_S17", "BAF60A_RA_2_S18",
"BAF60A_VD3_1_S15", "BAF60A_VD3_2_S16", "BAF60C_KD_1_UI_S1",
"BAF60C_KD_2_UI_S2", "BAF60C_KD_RA_1_S11", "BAF60C_KD_RA_2_S12",
"BAF60C_KD_VD3_1_S9", "BAF60C_KD_VD3_2_S10", "VC_1_UI_S3", "VC_2_UI_S4",
"VC_RA1_S5", "VC_RA_2_S6", "VC_VD3_1_S7", "VC_VD3_2_S8")
After removing the above pattern I want to remove this part from each of the colnames _S[anynumber]
so that I can end up like this as an example BAF60A_KD_1
but not sure how to make that pattern and match the same and remove or replace it with empty space. I tried as below which ends up like all the digits are removed.
colnames(countdata) = gsub("[[:digit:]_S]", "", colnames(countdata))
colnames(countdata)
c("BAFAKD", "BAFAKD", "BAFARA", "BAFARA", "BAFAVD", "BAFAVD",
"BAFCKDUI", "BAFCKDUI", "BAFCKDRA", "BAFCKDRA", "BAFCKDVD", "BAFCKDVD",
"VCUI", "VCUI", "VCRA", "VCRA", "VCVD", "VCVD")
Any suggestion would be helpful
names <- gsub("_S.*", "", colnames(countdata))
followed by removing any number or underscoregsub("[:digit:]|_", "", names)
$\endgroup$gsub
has aperl=TRUE
option for working with advanced regexes. Unfortunately, the[[:digit:]]
style regexes are not supported by the base regex engines in most utilities/distros. $\endgroup$[:digit:]
is a POSIX character class and should work on the vast majority of regex flavors. This isn't a feature of advanced regexes, but a feature of the most basic of them all: POSIX basic regular expressions. Where have you seen it not work? For example, it works in BRE (printf "foo\n123\n" | grep -P '[[:digit:]]'
), ERE (printf "foo\n123\n" | grep -E '[[:digit:]]'
), PCRE (printf "foo\n123\n" | grep -P '[[:digit:]]'
). $\endgroup$s/[[:digit:]]//
, which works on GNU sed without the-r
. The[:digit:]
is the special symbol, you need to put it in a character class ([ ]
, hence[[:digit:]]
) for it to work as a character class.s/[:digit:]//
shouldn't (and doesn't, on my GNU sed running on Linux) work even with the-r
(which just enables ERE). Even busybox sed (which is as basic as it gets) understands[[:digit:]]
without-r
. $\endgroup$