I have this dataframe:
structure(list(Treatnent.state = c("PRE Immune Checkpoint Blockade Therapy",
"PRE Immune Checkpoint Blockade Therapy", "PRE Immune Checkpoint Blockade Therapy",
"PRE Immune Checkpoint Blockade Therapy", "PRE Immune Checkpoint Blockade Therapy",
"PRE Immune Checkpoint Blockade Therapy", "PRE Immune Checkpoint Blockade Therapy (On dabrafenib+trametinib)",
"PRE Immune Checkpoint Blockade Therapy", "PRE Immune Checkpoint Blockade Therapy",
"PRE Immune Checkpoint Blockade Therapy", "PRE Immune Checkpoint Blockade Therapy",
"PRE Immune Checkpoint Blockade Therapy", "PRE Immune Checkpoint Blockade Therapy (On dabrafenib+trametinib)",
"PRE Immune Checkpoint Blockade Therapy (On dabrafenib+trametinib)"
), timepoint = c(-6, 0, 0, 0, 0, -1, 0, -3, -2, 0, 0, -1, 0,
0), Patient = c(115, 148, 208, 208, 272, 39, 42, 422, 62, 208,
208, 39, 42, 42)), class = "data.frame", row.names = c("115-031814 ",
"148-6-5-14_S9 ", "208-3-11-15_S13 ", "208-9-10-14_S11 ",
"272-121914 ", "39-3-31-14_S15 ", "42-10-17-14_S3 ",
"422-092815 ", "62-10-2-13_S6 ", "MGH208_031115-1.bam ",
"MGH208_031115-2.bam ", "MGH39_033114.bam ", "MGH42_101714.bam ",
"MGH42_101714_1.bam "))
with rownames:
[1] "115-031814 " "148-6-5-14_S9 " "208-3-11-15_S13 " "208-9-10-14_S11 "
[5] "272-121914 " "39-3-31-14_S15 " "42-10-17-14_S3 " "422-092815 "
[9] "62-10-2-13_S6 " "MGH208_031115-1.bam " "MGH208_031115-2.bam " "MGH39_033114.bam "
[13] "MGH42_101714.bam " "MGH42_101714_1.bam "
I want to add a prefix "X" and suffix ".bam", only for the rownames that don't start with MGH.
So for example: The rowname of the first row, 115-031814, would become X115-031814.bam, and the rowname MGH208_031115-1.bam would not change at all.
Use
greplto check whether a string starts with'MGH', thenifelseto applypaste"X" and ".bam" if it does not start with'MGH'. I usedtrimwsbecause some of your rownames has whitespace.output