Return the column name for the last value NA in row










2















I have a data frame containing condition assessments with a rating from 1-7 (as the column names). Each observation contains a representative area (as a percentage) for each condition rating (1-7). Each row should sum to 100% for each observation.



I am able to return the corresponding condition rating (1-7 from the column name) for the highest % coverage to show the majority of the area is condition x.



Here's my code to do this:



# Create some data:
set.seed(10)
df <- matrix(round(rbeta(100*7,1,1),digits=1), nc=7)
df <- round(sweep(df, 1, rowSums(df), FUN="/"),digits=1)
colnames(df)<-c(1:7) # Change the column names to reflect condition value
df <- as.data.frame(df)

# Now return the condition corresponding to the highest % coverage
df$maxPercCond <- as.numeric(colnames(df)[max.col(df,ties.method="last")])
df[df == 0] <- NA # Need to keep this as actual data contains NA values


My Question



I need to return the peak condition (pkVal) for each row.



 df[c(5,70),]

1 2 3 4 5 6 7 maxPercCond pkVal | pkVal(REQUIRED)
5 0.1 0.2 0.2 0.1 0.2 0.1 NA 5 0.1 | 6
70 0.2 0.2 0.1 0.2 0.1 NA 0.1 4 0.1 | 7


In the above example, pkVal should equal 6 and 7 respectively (as per my manual pkVal(REQUIRED) entry to show that 6 was the highest rated condition for the first row and 7 was the highest rated condition for the second row.



I've been trying a variation on the maxPerCond assignment but getting tied up in knots! Any suggestions/assistance would be most welcome:



 df$pkVal <- as.numeric(colnames(df)[max.col(df[cbind( 1:nrow(df),
max.col(!is.na(df[,1:7]),"last") )],ties.method="last")])









share|improve this question


























    2















    I have a data frame containing condition assessments with a rating from 1-7 (as the column names). Each observation contains a representative area (as a percentage) for each condition rating (1-7). Each row should sum to 100% for each observation.



    I am able to return the corresponding condition rating (1-7 from the column name) for the highest % coverage to show the majority of the area is condition x.



    Here's my code to do this:



    # Create some data:
    set.seed(10)
    df <- matrix(round(rbeta(100*7,1,1),digits=1), nc=7)
    df <- round(sweep(df, 1, rowSums(df), FUN="/"),digits=1)
    colnames(df)<-c(1:7) # Change the column names to reflect condition value
    df <- as.data.frame(df)

    # Now return the condition corresponding to the highest % coverage
    df$maxPercCond <- as.numeric(colnames(df)[max.col(df,ties.method="last")])
    df[df == 0] <- NA # Need to keep this as actual data contains NA values


    My Question



    I need to return the peak condition (pkVal) for each row.



     df[c(5,70),]

    1 2 3 4 5 6 7 maxPercCond pkVal | pkVal(REQUIRED)
    5 0.1 0.2 0.2 0.1 0.2 0.1 NA 5 0.1 | 6
    70 0.2 0.2 0.1 0.2 0.1 NA 0.1 4 0.1 | 7


    In the above example, pkVal should equal 6 and 7 respectively (as per my manual pkVal(REQUIRED) entry to show that 6 was the highest rated condition for the first row and 7 was the highest rated condition for the second row.



    I've been trying a variation on the maxPerCond assignment but getting tied up in knots! Any suggestions/assistance would be most welcome:



     df$pkVal <- as.numeric(colnames(df)[max.col(df[cbind( 1:nrow(df),
    max.col(!is.na(df[,1:7]),"last") )],ties.method="last")])









    share|improve this question
























      2












      2








      2








      I have a data frame containing condition assessments with a rating from 1-7 (as the column names). Each observation contains a representative area (as a percentage) for each condition rating (1-7). Each row should sum to 100% for each observation.



      I am able to return the corresponding condition rating (1-7 from the column name) for the highest % coverage to show the majority of the area is condition x.



      Here's my code to do this:



      # Create some data:
      set.seed(10)
      df <- matrix(round(rbeta(100*7,1,1),digits=1), nc=7)
      df <- round(sweep(df, 1, rowSums(df), FUN="/"),digits=1)
      colnames(df)<-c(1:7) # Change the column names to reflect condition value
      df <- as.data.frame(df)

      # Now return the condition corresponding to the highest % coverage
      df$maxPercCond <- as.numeric(colnames(df)[max.col(df,ties.method="last")])
      df[df == 0] <- NA # Need to keep this as actual data contains NA values


      My Question



      I need to return the peak condition (pkVal) for each row.



       df[c(5,70),]

      1 2 3 4 5 6 7 maxPercCond pkVal | pkVal(REQUIRED)
      5 0.1 0.2 0.2 0.1 0.2 0.1 NA 5 0.1 | 6
      70 0.2 0.2 0.1 0.2 0.1 NA 0.1 4 0.1 | 7


      In the above example, pkVal should equal 6 and 7 respectively (as per my manual pkVal(REQUIRED) entry to show that 6 was the highest rated condition for the first row and 7 was the highest rated condition for the second row.



      I've been trying a variation on the maxPerCond assignment but getting tied up in knots! Any suggestions/assistance would be most welcome:



       df$pkVal <- as.numeric(colnames(df)[max.col(df[cbind( 1:nrow(df),
      max.col(!is.na(df[,1:7]),"last") )],ties.method="last")])









      share|improve this question














      I have a data frame containing condition assessments with a rating from 1-7 (as the column names). Each observation contains a representative area (as a percentage) for each condition rating (1-7). Each row should sum to 100% for each observation.



      I am able to return the corresponding condition rating (1-7 from the column name) for the highest % coverage to show the majority of the area is condition x.



      Here's my code to do this:



      # Create some data:
      set.seed(10)
      df <- matrix(round(rbeta(100*7,1,1),digits=1), nc=7)
      df <- round(sweep(df, 1, rowSums(df), FUN="/"),digits=1)
      colnames(df)<-c(1:7) # Change the column names to reflect condition value
      df <- as.data.frame(df)

      # Now return the condition corresponding to the highest % coverage
      df$maxPercCond <- as.numeric(colnames(df)[max.col(df,ties.method="last")])
      df[df == 0] <- NA # Need to keep this as actual data contains NA values


      My Question



      I need to return the peak condition (pkVal) for each row.



       df[c(5,70),]

      1 2 3 4 5 6 7 maxPercCond pkVal | pkVal(REQUIRED)
      5 0.1 0.2 0.2 0.1 0.2 0.1 NA 5 0.1 | 6
      70 0.2 0.2 0.1 0.2 0.1 NA 0.1 4 0.1 | 7


      In the above example, pkVal should equal 6 and 7 respectively (as per my manual pkVal(REQUIRED) entry to show that 6 was the highest rated condition for the first row and 7 was the highest rated condition for the second row.



      I've been trying a variation on the maxPerCond assignment but getting tied up in knots! Any suggestions/assistance would be most welcome:



       df$pkVal <- as.numeric(colnames(df)[max.col(df[cbind( 1:nrow(df),
      max.col(!is.na(df[,1:7]),"last") )],ties.method="last")])






      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 13:50









      NickNick

      386




      386






















          1 Answer
          1






          active

          oldest

          votes


















          2














          One option might be to use apply in row mode and find the column name of the corresponding last element which is not equal to NA:



          apply(df, 1, function(x) tail(names(x)[!is.na(x)], n=1) )





          share|improve this answer























          • Thank you Tim, works perfectly.

            – Nick
            Nov 13 '18 at 14:35










          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',
          autoActivateHeartbeat: false,
          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%2f53282518%2freturn-the-column-name-for-the-last-value-na-in-row%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









          2














          One option might be to use apply in row mode and find the column name of the corresponding last element which is not equal to NA:



          apply(df, 1, function(x) tail(names(x)[!is.na(x)], n=1) )





          share|improve this answer























          • Thank you Tim, works perfectly.

            – Nick
            Nov 13 '18 at 14:35















          2














          One option might be to use apply in row mode and find the column name of the corresponding last element which is not equal to NA:



          apply(df, 1, function(x) tail(names(x)[!is.na(x)], n=1) )





          share|improve this answer























          • Thank you Tim, works perfectly.

            – Nick
            Nov 13 '18 at 14:35













          2












          2








          2







          One option might be to use apply in row mode and find the column name of the corresponding last element which is not equal to NA:



          apply(df, 1, function(x) tail(names(x)[!is.na(x)], n=1) )





          share|improve this answer













          One option might be to use apply in row mode and find the column name of the corresponding last element which is not equal to NA:



          apply(df, 1, function(x) tail(names(x)[!is.na(x)], n=1) )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 13:56









          Tim BiegeleisenTim Biegeleisen

          226k1392145




          226k1392145












          • Thank you Tim, works perfectly.

            – Nick
            Nov 13 '18 at 14:35

















          • Thank you Tim, works perfectly.

            – Nick
            Nov 13 '18 at 14:35
















          Thank you Tim, works perfectly.

          – Nick
          Nov 13 '18 at 14:35





          Thank you Tim, works perfectly.

          – Nick
          Nov 13 '18 at 14:35



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282518%2freturn-the-column-name-for-the-last-value-na-in-row%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