Replacing values in a dataframe by row in R









up vote
1
down vote

favorite












Is there a way in R to replace values in each row of a matrix/dataframe with a specific value from that row?



For example, I have the following matrix:



df<-cbind(c("A","C","G","T"),c("T","G","C","A"),c(0,1,0,1),c(1,0,1,0),c(0,1,0,1))

df
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "0" "1" "0"
#[2,] "C" "G" "1" "0" "1"
#[3,] "G" "C" "0" "1" "0"
#[4,] "T" "A" "1" "0" "1"


and I want to replace the zeros in each row with the corresponding letter from the first column of that row, such that the new matrix will look like this:



newdf
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "A" "1" "A"
#[2,] "C" "G" "1" "C" "1"
#[3,] "G" "C" "G" "1" "G"
#[4,] "T" "A" "1" "T" "1"


The closest I have been able to get is with the following commands, but it does not replace the zeros with the correct values from column 1.



df[df==0]<-NA
df[, 3:ncol(df)][is.na(df[, 3:ncol(df)])] <- df[,1]









share|improve this question





















  • Try t(apply(df, 1, function(x) replace(x, x == 0, x[1])))
    – akrun
    Nov 9 at 19:31










  • Thanks @akrun this works great!
    – Arkol258
    Nov 9 at 19:41














up vote
1
down vote

favorite












Is there a way in R to replace values in each row of a matrix/dataframe with a specific value from that row?



For example, I have the following matrix:



df<-cbind(c("A","C","G","T"),c("T","G","C","A"),c(0,1,0,1),c(1,0,1,0),c(0,1,0,1))

df
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "0" "1" "0"
#[2,] "C" "G" "1" "0" "1"
#[3,] "G" "C" "0" "1" "0"
#[4,] "T" "A" "1" "0" "1"


and I want to replace the zeros in each row with the corresponding letter from the first column of that row, such that the new matrix will look like this:



newdf
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "A" "1" "A"
#[2,] "C" "G" "1" "C" "1"
#[3,] "G" "C" "G" "1" "G"
#[4,] "T" "A" "1" "T" "1"


The closest I have been able to get is with the following commands, but it does not replace the zeros with the correct values from column 1.



df[df==0]<-NA
df[, 3:ncol(df)][is.na(df[, 3:ncol(df)])] <- df[,1]









share|improve this question





















  • Try t(apply(df, 1, function(x) replace(x, x == 0, x[1])))
    – akrun
    Nov 9 at 19:31










  • Thanks @akrun this works great!
    – Arkol258
    Nov 9 at 19:41












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Is there a way in R to replace values in each row of a matrix/dataframe with a specific value from that row?



For example, I have the following matrix:



df<-cbind(c("A","C","G","T"),c("T","G","C","A"),c(0,1,0,1),c(1,0,1,0),c(0,1,0,1))

df
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "0" "1" "0"
#[2,] "C" "G" "1" "0" "1"
#[3,] "G" "C" "0" "1" "0"
#[4,] "T" "A" "1" "0" "1"


and I want to replace the zeros in each row with the corresponding letter from the first column of that row, such that the new matrix will look like this:



newdf
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "A" "1" "A"
#[2,] "C" "G" "1" "C" "1"
#[3,] "G" "C" "G" "1" "G"
#[4,] "T" "A" "1" "T" "1"


The closest I have been able to get is with the following commands, but it does not replace the zeros with the correct values from column 1.



df[df==0]<-NA
df[, 3:ncol(df)][is.na(df[, 3:ncol(df)])] <- df[,1]









share|improve this question













Is there a way in R to replace values in each row of a matrix/dataframe with a specific value from that row?



For example, I have the following matrix:



df<-cbind(c("A","C","G","T"),c("T","G","C","A"),c(0,1,0,1),c(1,0,1,0),c(0,1,0,1))

df
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "0" "1" "0"
#[2,] "C" "G" "1" "0" "1"
#[3,] "G" "C" "0" "1" "0"
#[4,] "T" "A" "1" "0" "1"


and I want to replace the zeros in each row with the corresponding letter from the first column of that row, such that the new matrix will look like this:



newdf
# [,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "A" "1" "A"
#[2,] "C" "G" "1" "C" "1"
#[3,] "G" "C" "G" "1" "G"
#[4,] "T" "A" "1" "T" "1"


The closest I have been able to get is with the following commands, but it does not replace the zeros with the correct values from column 1.



df[df==0]<-NA
df[, 3:ncol(df)][is.na(df[, 3:ncol(df)])] <- df[,1]






r dataframe matrix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:30









Arkol258

102




102











  • Try t(apply(df, 1, function(x) replace(x, x == 0, x[1])))
    – akrun
    Nov 9 at 19:31










  • Thanks @akrun this works great!
    – Arkol258
    Nov 9 at 19:41
















  • Try t(apply(df, 1, function(x) replace(x, x == 0, x[1])))
    – akrun
    Nov 9 at 19:31










  • Thanks @akrun this works great!
    – Arkol258
    Nov 9 at 19:41















Try t(apply(df, 1, function(x) replace(x, x == 0, x[1])))
– akrun
Nov 9 at 19:31




Try t(apply(df, 1, function(x) replace(x, x == 0, x[1])))
– akrun
Nov 9 at 19:31












Thanks @akrun this works great!
– Arkol258
Nov 9 at 19:41




Thanks @akrun this works great!
– Arkol258
Nov 9 at 19:41












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










We can replicate the first column to make the lengths equal and then do the assignment based on the logical matrix. It will subset the elements that are of the same length as in the rhs



i1 <- df == 0
newdf <- df
newdf[i1] <- df[,1][row(df)][i1]
newdf
[,1] [,2] [,3] [,4] [,5]
#[1,] "A" "T" "A" "1" "A"
#[2,] "C" "G" "1" "C" "1"
#[3,] "G" "C" "G" "1" "G"
#[4,] "T" "A" "1" "T" "1"





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232182%2freplacing-values-in-a-dataframe-by-row-in-r%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    We can replicate the first column to make the lengths equal and then do the assignment based on the logical matrix. It will subset the elements that are of the same length as in the rhs



    i1 <- df == 0
    newdf <- df
    newdf[i1] <- df[,1][row(df)][i1]
    newdf
    [,1] [,2] [,3] [,4] [,5]
    #[1,] "A" "T" "A" "1" "A"
    #[2,] "C" "G" "1" "C" "1"
    #[3,] "G" "C" "G" "1" "G"
    #[4,] "T" "A" "1" "T" "1"





    share|improve this answer
























      up vote
      0
      down vote



      accepted










      We can replicate the first column to make the lengths equal and then do the assignment based on the logical matrix. It will subset the elements that are of the same length as in the rhs



      i1 <- df == 0
      newdf <- df
      newdf[i1] <- df[,1][row(df)][i1]
      newdf
      [,1] [,2] [,3] [,4] [,5]
      #[1,] "A" "T" "A" "1" "A"
      #[2,] "C" "G" "1" "C" "1"
      #[3,] "G" "C" "G" "1" "G"
      #[4,] "T" "A" "1" "T" "1"





      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        We can replicate the first column to make the lengths equal and then do the assignment based on the logical matrix. It will subset the elements that are of the same length as in the rhs



        i1 <- df == 0
        newdf <- df
        newdf[i1] <- df[,1][row(df)][i1]
        newdf
        [,1] [,2] [,3] [,4] [,5]
        #[1,] "A" "T" "A" "1" "A"
        #[2,] "C" "G" "1" "C" "1"
        #[3,] "G" "C" "G" "1" "G"
        #[4,] "T" "A" "1" "T" "1"





        share|improve this answer












        We can replicate the first column to make the lengths equal and then do the assignment based on the logical matrix. It will subset the elements that are of the same length as in the rhs



        i1 <- df == 0
        newdf <- df
        newdf[i1] <- df[,1][row(df)][i1]
        newdf
        [,1] [,2] [,3] [,4] [,5]
        #[1,] "A" "T" "A" "1" "A"
        #[2,] "C" "G" "1" "C" "1"
        #[3,] "G" "C" "G" "1" "G"
        #[4,] "T" "A" "1" "T" "1"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 19:33









        akrun

        389k13177250




        389k13177250



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232182%2freplacing-values-in-a-dataframe-by-row-in-r%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo