panbar panbar - 4 months ago 8
R Question

Replace partial values from a column in a R dataframe

I have a dataframe, example as follows.

chr start2 end2 value
88 chrom16 56063633 56063634 0.238
78 chrom12 83039622 83039623 0.429
50 chrom12 73209081 73209082 0.313
68 chrom12 75138610 75138611 0.679
45 chrom12 67566601 67566602 0.859
120 chrom16 57694245 57694246 0.438


I would like to change partial values from a column. In this dataframe, I would like to change "chrom" to "chr" in column 2.

OUTPUT should look like

chr start2 end2 value
88 chr16 56063633 56063634 0.238
78 chr12 83039622 83039623 0.429
50 chr12 73209081 73209082 0.313
68 chr12 75138610 75138611 0.679
45 chr12 67566601 67566602 0.859
120 chr16 57694245 57694246 0.438

Answer

If it the pattern is as simple as in your example you can just replace the "om" in "chrom" with an empty string.

df <- read.table(text = " chr   start2     end2 value
                              88  chrom16 56063633 56063634 0.238
                              78  chrom12 83039622 83039623 0.429
                              50  chrom12 73209081 73209082 0.313
                              68  chrom12 75138610 75138611 0.679
                              45  chrom12 67566601 67566602 0.859
                              120 chrom16 57694245 57694246 0.438", header = TRUE)

df$chr <- sub("om", "", df$chr)

df
#          chr   start2     end2 value
#    88  chr16 56063633 56063634 0.238
#    78  chr12 83039622 83039623 0.429
#    50  chr12 73209081 73209082 0.313
#    68  chr12 75138610 75138611 0.679
#    45  chr12 67566601 67566602 0.859
#    120 chr16 57694245 57694246 0.438