0

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?

Evan
  • 1,477
  • 1
  • 17
  • 34
  • What did you try so far? Also, there are now plenty of questions about "searching/replacing with R" in this site. What did you search and find? –  Dec 04 '15 at 05:24
  • @Pascal Actually Evan isn't even replacing, he is simply adding a prefix. – Tim Biegeleisen Dec 04 '15 at 05:24
  • 1
    Possible duplicate of [How can 2 strings be concatenated in R](http://stackoverflow.com/questions/7201341/how-can-2-strings-be-concatenated-in-r) – Andy Clifton Dec 04 '15 at 05:27
  • 1
    @TimBiegeleisen Yes, I was abused by the title. But still the need for OP for searching by him/her self first applies. –  Dec 04 '15 at 05:32

1 Answers1

3

Just use paste():

df$CHR <- as.character(df$CHR)          # in case it is a factor column
df$CHR <- paste("chr", df$CHR, sep="")
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360