Evan
Evan

Reputation: 1499

Replace a certain number with a phrase in R

I have a data.frame that looks like

    SNP            CHR         BP          A1      A2        EFF     SE      P      NGT
    rs882982        1       100066094       a       g       -.0179  .006    .002797 28      .486
    rs7518025       2       100066515       t       c       .0198   .0059   .0007438        26      .47
    rs6678322       3       100069554       a       t       .0187   .0059   .001498 25      .452
    rs61784986      14       100074953       t       c       -.0182  .0058   .001748 26      .469
    rs7554246       21      100075695       t       c       -.0167  .006    .004932 26      .455
    rs12121193      16      100075777       a       t       -.0183  .0058   .001652 26      .471
    rs835016        3      100078102       t       c       .02     .0065   .001979 28      .196

And I would like to add the letters "chr" before the numbers in the CHR column. My desired output is:

    SNP            CHR         BP          A1      A2        EFF     SE      P      NGT
    rs882982        chr1       100066094       a       g       -.0179  .006    .002797 28      .486
    rs7518025       chr2       100066515       t       c       .0198   .0059   .0007438        26      .47
    rs6678322       chr3       100069554       a       t       .0187   .0059   .001498 25      .452
    rs61784986      chr14       100074953       t       c       -.0182  .0058   .001748 26      .469
    rs7554246       chr21      100075695       t       c       -.0167  .006    .004932 26      .455
    rs12121193      chr16      100075777       a       t       -.0183  .0058   .001652 26      .471
    rs835016        chr3      100078102       t       c       .02     .0065   .001979 28      .196

Should I use grep somehow, or what are some useful R commands?

Upvotes: 0

Views: 49

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520978

Just use paste():

df$CHR <- as.character(df$CHR)          # in case it is a factor column
df$CHR <- paste("chr", df$CHR, sep="")

Upvotes: 3

Related Questions