Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove part of string from column names [closed]

Tags:

regex

r

That's a data:

structure(list(Fasta.headers = c("Person01050.1", "Person01080.1", 
                                 "Person01090.1", "Person01100.4", "Person01140.1", "Person01220.1"), 
               ToRemove.Gr_1 = c(0, 1107200, 17096000, 0, 0, 0), ToRemove.Gr_10 = c(0, 
                                                                                      37259000, 1104800000, 783870, 0, 1308600), ToRemove.Gr_11 = c(1835800, 
                                                                                                                                                     53909000, 623960000, 0, 0, 0), ToRemove.Gr_12 = c(0, 19117000, 
                                                                                                                                                                                                        808600000, 0, 0, 719400), ToRemove.Gr_13 = c(2544200, 2461400, 
                                                                                                                                                                                                                                                      418770000, 0, 0, 0), ToRemove.Gr_14 = c(5120400, 1373700, 
                                                                                                                                                                                                                                                                                               117330000, 0, 0, 0), ToRemove.Gr_15 = c(6623500, 0, 73336000, 
                                                                                                                                                                                                                                                                                                                                        0, 0, 0), ToRemove.Gr_16 = c(0, 0, 31761000, 0, 0, 0), ToRemove.Gr_17 = c(13475000, 
                                                                                                                                                                                                                                                                                                                                                                                                                    0, 29387000, 0, 0, 0), ToRemove.Gr_18 = c(7883300, 0, 27476000, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                               0, 0, 0), ToRemove.Gr_19 = c(82339000, 3254700, 50825000, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             0, 0, 0), ToRemove.Gr_2 = c(1584100, 84847000, 5219500000, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          6860700, 0, 8337700), ToRemove.Gr_20 = c(205860000, 0, 67685000, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    0, 0, 0), ToRemove.Gr_21 = c(867120000, 1984400, 2.26e+08, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  0, 0, 10502000)), .Names = c("Fasta.headers", "ToRemove.Gr_1", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "ToRemove.Gr_10", "ToRemove.Gr_11", "ToRemove.Gr_12", "ToRemove.Gr_13", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "ToRemove.Gr_14", "ToRemove.Gr_15", "ToRemove.Gr_16", "ToRemove.Gr_17", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "ToRemove.Gr_18", "ToRemove.Gr_19", "ToRemove.Gr_2", "ToRemove.Gr_20", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "ToRemove.Gr_21"), row.names = c(NA, 6L), class = "data.frame")

As already column names suggests part "ToRemove" should be removed from the name and only Gr_* should stay behind.

I would appreciate two solutions for that problem. First based on a assigned string it should delete part of column name or based on specific character like . for example. It should remove whole part before or after a dot.

like image 618
Shaxi Liver Avatar asked Mar 07 '23 22:03

Shaxi Liver


1 Answers

We can use sub

names(df1)[-1] <- sub(".*\\.", "", names(df1)[-1])

If we need the . as well, replace with .

names(df1)[-1] <- sub(".*\\.", ".", names(df1)[-1])

To match the pattern exactly, we can also match zero or more characters that are not a do t([^.]*) from the start (^) of the string followed by a dot (\\. - escape the dot as it is a metacharacter implying any character) and replace it with blank ("")

sub("^[^.]*\\.", "", names(df1)[-1])
#[1] "Gr_1"  "Gr_10" "Gr_11" "Gr_12" "Gr_13" "Gr_14" "Gr_15" "Gr_16" 
#[9] "Gr_17" "Gr_18" "Gr_19" "Gr_2"  "Gr_20" "Gr_21"

As it is already mentioned above 'ToRemove',

sub("ToRemove.", "", names(df1)[-1], fixed = TRUE)

Also, if we need to remove all characters including .

sub("\\..*", "", names(df1)[-1])
like image 106
akrun Avatar answered Mar 10 '23 11:03

akrun