2
$\begingroup$
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

$\endgroup$
9
  • 6
    $\begingroup$ I'm voting to close this question as off-topic because the question is purely about regex, not requiring any biological knowledge. You can remove them with names <- gsub("_S.*", "", colnames(countdata)) followed by removing any number or underscore gsub("[:digit:]|_", "", names) $\endgroup$
    – llrs
    Commented Apr 3, 2019 at 12:51
  • $\begingroup$ IIRC gsub has a perl=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$
    – Ram RS
    Commented Apr 3, 2019 at 14:22
  • $\begingroup$ @RamRS what do you mean? [: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$
    – terdon
    Commented Apr 3, 2019 at 16:08
  • 3
    $\begingroup$ @llrs I don't think this is off topic. Bioinformatics involves a lot of basic programming knowledge, and I would say that includes regular expressions. You can do a lot of very productive work in bioinformatics without really knowing the first thing about biology. I would say this comes under the same category as general text parsing questions which we've already decided should be on topic. $\endgroup$
    – terdon
    Commented Apr 3, 2019 at 16:10
  • 1
    $\begingroup$ @RamRS That should be 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$
    – terdon
    Commented Apr 3, 2019 at 17:59

1 Answer 1

3
$\begingroup$

Without regex, split on "_", drop last item, then paste it back again:

sapply(strsplit(x, "_"), function(i) paste(head(i, -1), collapse = "_"))

Or with regex (stealing the code from tools::file_path_sans_ext function which is used to drop file extensions):

sub("([^_]+)\\_[[:alnum:]]+$", "\\1", x)

# [1] "BAF60A_KD_1"     "BAF60A_KD_2"     "BAF60A_RA_1"     "BAF60A_RA_2"     "BAF60A_VD3_1"    "BAF60A_VD3_2"   
# [7] "BAF60C_KD_1_UI"  "BAF60C_KD_2_UI"  "BAF60C_KD_RA_1"  "BAF60C_KD_RA_2"  "BAF60C_KD_VD3_1" "BAF60C_KD_VD3_2"
# [13] "VC_1_UI"         "VC_2_UI"         "VC_RA1"          "VC_RA_2"         "VC_VD3_1"        "VC_VD3_2" 
$\endgroup$
8
  • $\begingroup$ multiple ways of doing wow $\endgroup$
    – kcm
    Commented Apr 4, 2019 at 12:27
  • $\begingroup$ @krushnachChandra minor note, you will get a quicker and better answers if you post regex/R posts at StackOverflow. $\endgroup$
    – zx8754
    Commented Apr 4, 2019 at 12:32
  • $\begingroup$ well actually i got banned from posting it on the R forum ,i guess i asked stupid questions.it says from this account no more questions.But im glad for multiple answers coming out $\endgroup$
    – kcm
    Commented Apr 4, 2019 at 12:36
  • $\begingroup$ What do you mean "banned"? This is your profile, right? stackoverflow.com/users/6342356/krushnach-chandra $\endgroup$
    – zx8754
    Commented Apr 4, 2019 at 12:38
  • $\begingroup$ yes i do ask question on this forum and other forum but not in the R section may be bad questions $\endgroup$
    – kcm
    Commented Apr 4, 2019 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.