Difference in values between different columns satisfying a given condition












2















Here is my toy data. I have val and quartile variables q0 to q4.



 df <- tibble::tribble(
~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )


I need to calculate the last two variables such that:




  1. When val is between q1 and q2, I pick 2 (of q2) for variable q (2nd
    row)

  2. If there is a tie, I pick the max of qs (eg. q = 4 in 1st row)

  3. diff is the difference between q and val. So, for row 1, it's q4-val = 0 and for row 2, it's q2 - val = 30 - 17 = 13.


How can I calculate q and diff in R, preferably using tidyverse? May be we can leverage answers here: Extract column name and specific value based on a condition.










share|improve this question



























    2















    Here is my toy data. I have val and quartile variables q0 to q4.



     df <- tibble::tribble(
    ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
    15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
    17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
    29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
    25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )


    I need to calculate the last two variables such that:




    1. When val is between q1 and q2, I pick 2 (of q2) for variable q (2nd
      row)

    2. If there is a tie, I pick the max of qs (eg. q = 4 in 1st row)

    3. diff is the difference between q and val. So, for row 1, it's q4-val = 0 and for row 2, it's q2 - val = 30 - 17 = 13.


    How can I calculate q and diff in R, preferably using tidyverse? May be we can leverage answers here: Extract column name and specific value based on a condition.










    share|improve this question

























      2












      2








      2








      Here is my toy data. I have val and quartile variables q0 to q4.



       df <- tibble::tribble(
      ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
      15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
      17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
      29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
      25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )


      I need to calculate the last two variables such that:




      1. When val is between q1 and q2, I pick 2 (of q2) for variable q (2nd
        row)

      2. If there is a tie, I pick the max of qs (eg. q = 4 in 1st row)

      3. diff is the difference between q and val. So, for row 1, it's q4-val = 0 and for row 2, it's q2 - val = 30 - 17 = 13.


      How can I calculate q and diff in R, preferably using tidyverse? May be we can leverage answers here: Extract column name and specific value based on a condition.










      share|improve this question














      Here is my toy data. I have val and quartile variables q0 to q4.



       df <- tibble::tribble(
      ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
      15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
      17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
      29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
      25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )


      I need to calculate the last two variables such that:




      1. When val is between q1 and q2, I pick 2 (of q2) for variable q (2nd
        row)

      2. If there is a tie, I pick the max of qs (eg. q = 4 in 1st row)

      3. diff is the difference between q and val. So, for row 1, it's q4-val = 0 and for row 2, it's q2 - val = 30 - 17 = 13.


      How can I calculate q and diff in R, preferably using tidyverse? May be we can leverage answers here: Extract column name and specific value based on a condition.







      r tidyverse






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 16:37









      GeetGeet

      5611717




      5611717
























          2 Answers
          2






          active

          oldest

          votes


















          2














          When you have more complicated logic like this, I find it's usually better to wrap it in a function. It will be easier to maintain, read, and debug in the future. I'd also be extra careful when using a lot of nested ifelse kind of statements or a big case_when type of thing. In the accepted answer q can only be 2, 3, or 4. There is no case provided for q to be 1, which you certainly want as an option in your final product.



          df <- tibble::tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
          15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
          17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
          29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
          25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )

          whichQ <- function(df, qs = c('q0', 'q1', 'q2', 'q3', 'q4')) {
          # This has the flexibility of changing your column names / using more or less Q splits
          qDf <- df[, qs]
          # This finds the right quantile by finding how many you are larger than
          # It works because the q's are sequential
          whichGreater <- df$val >= qDf
          q <- apply(whichGreater, 1, sum)
          # 4 is a special case because there is no next quantile
          q <- ifelse(q == 5, 4, q)
          df$q <- q
          # Go through the Qs we found and grab the value of that column
          diff <- sapply(seq_along(q), function(x) {
          as.integer(qDf[x, q[x]+1])
          })
          # Get the difference
          df$diff <- diff - df$val
          df
          }


          You can still use this with tidyverse piping, but it is more clear (I think) what's happening as long as you name your function something useful.



          df %>% 
          whichQ %>%
          head(2)





          share|improve this answer



















          • 1





            Wow...that's nice! Thanks a lot, Taiki Sakai. Really appreciate it!

            – Geet
            Nov 14 '18 at 18:47



















          1














          Try out:



          library(tidyverse)
          df <- tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4,
          15L, 15L, 15L, 15L, 15, 15L,
          17L, 2L, 16L, 30L, 34, 54L,
          29L, 2L, 16L, 30L, 34, 54L,
          25L, 2L, 17L, 20L, 26, 43L)

          df %>%
          mutate(q = ifelse(val > q1 & val < q2, 2,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, 4,
          3)),
          diff = ifelse(val > q1 & val < q2, q2 - val,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, q4 - val,
          q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <dbl>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1


          With case_when (assuming that when val is between q2 and q3, you pick 3).



          df %>%
          mutate(q = case_when(val > q1 & val < q2 ~ 2,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ 4,
          val > q2 & val < q3 ~ 3),
          diff = case_when(val > q1 & val < q2 ~ q2 - val,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ q4 - val,
          val > q2 & val < q3 ~ as.integer(q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <int>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1





          share|improve this answer





















          • 1





            I guess, case_when will also work!

            – Geet
            Nov 13 '18 at 17:42











          • Of course, but you should first specify a clear condition to assign 3 to q

            – ANG
            Nov 13 '18 at 17:46











          • Fantastic! Thank you, ANG!

            – Geet
            Nov 13 '18 at 19:06











          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%2f53285617%2fdifference-in-values-between-different-columns-satisfying-a-given-condition%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          When you have more complicated logic like this, I find it's usually better to wrap it in a function. It will be easier to maintain, read, and debug in the future. I'd also be extra careful when using a lot of nested ifelse kind of statements or a big case_when type of thing. In the accepted answer q can only be 2, 3, or 4. There is no case provided for q to be 1, which you certainly want as an option in your final product.



          df <- tibble::tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
          15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
          17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
          29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
          25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )

          whichQ <- function(df, qs = c('q0', 'q1', 'q2', 'q3', 'q4')) {
          # This has the flexibility of changing your column names / using more or less Q splits
          qDf <- df[, qs]
          # This finds the right quantile by finding how many you are larger than
          # It works because the q's are sequential
          whichGreater <- df$val >= qDf
          q <- apply(whichGreater, 1, sum)
          # 4 is a special case because there is no next quantile
          q <- ifelse(q == 5, 4, q)
          df$q <- q
          # Go through the Qs we found and grab the value of that column
          diff <- sapply(seq_along(q), function(x) {
          as.integer(qDf[x, q[x]+1])
          })
          # Get the difference
          df$diff <- diff - df$val
          df
          }


          You can still use this with tidyverse piping, but it is more clear (I think) what's happening as long as you name your function something useful.



          df %>% 
          whichQ %>%
          head(2)





          share|improve this answer



















          • 1





            Wow...that's nice! Thanks a lot, Taiki Sakai. Really appreciate it!

            – Geet
            Nov 14 '18 at 18:47
















          2














          When you have more complicated logic like this, I find it's usually better to wrap it in a function. It will be easier to maintain, read, and debug in the future. I'd also be extra careful when using a lot of nested ifelse kind of statements or a big case_when type of thing. In the accepted answer q can only be 2, 3, or 4. There is no case provided for q to be 1, which you certainly want as an option in your final product.



          df <- tibble::tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
          15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
          17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
          29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
          25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )

          whichQ <- function(df, qs = c('q0', 'q1', 'q2', 'q3', 'q4')) {
          # This has the flexibility of changing your column names / using more or less Q splits
          qDf <- df[, qs]
          # This finds the right quantile by finding how many you are larger than
          # It works because the q's are sequential
          whichGreater <- df$val >= qDf
          q <- apply(whichGreater, 1, sum)
          # 4 is a special case because there is no next quantile
          q <- ifelse(q == 5, 4, q)
          df$q <- q
          # Go through the Qs we found and grab the value of that column
          diff <- sapply(seq_along(q), function(x) {
          as.integer(qDf[x, q[x]+1])
          })
          # Get the difference
          df$diff <- diff - df$val
          df
          }


          You can still use this with tidyverse piping, but it is more clear (I think) what's happening as long as you name your function something useful.



          df %>% 
          whichQ %>%
          head(2)





          share|improve this answer



















          • 1





            Wow...that's nice! Thanks a lot, Taiki Sakai. Really appreciate it!

            – Geet
            Nov 14 '18 at 18:47














          2












          2








          2







          When you have more complicated logic like this, I find it's usually better to wrap it in a function. It will be easier to maintain, read, and debug in the future. I'd also be extra careful when using a lot of nested ifelse kind of statements or a big case_when type of thing. In the accepted answer q can only be 2, 3, or 4. There is no case provided for q to be 1, which you certainly want as an option in your final product.



          df <- tibble::tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
          15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
          17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
          29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
          25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )

          whichQ <- function(df, qs = c('q0', 'q1', 'q2', 'q3', 'q4')) {
          # This has the flexibility of changing your column names / using more or less Q splits
          qDf <- df[, qs]
          # This finds the right quantile by finding how many you are larger than
          # It works because the q's are sequential
          whichGreater <- df$val >= qDf
          q <- apply(whichGreater, 1, sum)
          # 4 is a special case because there is no next quantile
          q <- ifelse(q == 5, 4, q)
          df$q <- q
          # Go through the Qs we found and grab the value of that column
          diff <- sapply(seq_along(q), function(x) {
          as.integer(qDf[x, q[x]+1])
          })
          # Get the difference
          df$diff <- diff - df$val
          df
          }


          You can still use this with tidyverse piping, but it is more clear (I think) what's happening as long as you name your function something useful.



          df %>% 
          whichQ %>%
          head(2)





          share|improve this answer













          When you have more complicated logic like this, I find it's usually better to wrap it in a function. It will be easier to maintain, read, and debug in the future. I'd also be extra careful when using a lot of nested ifelse kind of statements or a big case_when type of thing. In the accepted answer q can only be 2, 3, or 4. There is no case provided for q to be 1, which you certainly want as an option in your final product.



          df <- tibble::tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4, ~q, ~diff,
          15L, 15L, 15L, 15L, 15, 15L, 4L, 0,
          17L, 2L, 16L, 30L, 34, 54L, 2L, 13,
          29L, 2L, 16L, 30L, 34, 54L, 2L, 1,
          25L, 2L, 17L, 20L, 26, 43L, 3L, 1 )

          whichQ <- function(df, qs = c('q0', 'q1', 'q2', 'q3', 'q4')) {
          # This has the flexibility of changing your column names / using more or less Q splits
          qDf <- df[, qs]
          # This finds the right quantile by finding how many you are larger than
          # It works because the q's are sequential
          whichGreater <- df$val >= qDf
          q <- apply(whichGreater, 1, sum)
          # 4 is a special case because there is no next quantile
          q <- ifelse(q == 5, 4, q)
          df$q <- q
          # Go through the Qs we found and grab the value of that column
          diff <- sapply(seq_along(q), function(x) {
          as.integer(qDf[x, q[x]+1])
          })
          # Get the difference
          df$diff <- diff - df$val
          df
          }


          You can still use this with tidyverse piping, but it is more clear (I think) what's happening as long as you name your function something useful.



          df %>% 
          whichQ %>%
          head(2)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 19:40









          Taiki SakaiTaiki Sakai

          35114




          35114








          • 1





            Wow...that's nice! Thanks a lot, Taiki Sakai. Really appreciate it!

            – Geet
            Nov 14 '18 at 18:47














          • 1





            Wow...that's nice! Thanks a lot, Taiki Sakai. Really appreciate it!

            – Geet
            Nov 14 '18 at 18:47








          1




          1





          Wow...that's nice! Thanks a lot, Taiki Sakai. Really appreciate it!

          – Geet
          Nov 14 '18 at 18:47





          Wow...that's nice! Thanks a lot, Taiki Sakai. Really appreciate it!

          – Geet
          Nov 14 '18 at 18:47













          1














          Try out:



          library(tidyverse)
          df <- tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4,
          15L, 15L, 15L, 15L, 15, 15L,
          17L, 2L, 16L, 30L, 34, 54L,
          29L, 2L, 16L, 30L, 34, 54L,
          25L, 2L, 17L, 20L, 26, 43L)

          df %>%
          mutate(q = ifelse(val > q1 & val < q2, 2,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, 4,
          3)),
          diff = ifelse(val > q1 & val < q2, q2 - val,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, q4 - val,
          q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <dbl>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1


          With case_when (assuming that when val is between q2 and q3, you pick 3).



          df %>%
          mutate(q = case_when(val > q1 & val < q2 ~ 2,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ 4,
          val > q2 & val < q3 ~ 3),
          diff = case_when(val > q1 & val < q2 ~ q2 - val,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ q4 - val,
          val > q2 & val < q3 ~ as.integer(q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <int>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1





          share|improve this answer





















          • 1





            I guess, case_when will also work!

            – Geet
            Nov 13 '18 at 17:42











          • Of course, but you should first specify a clear condition to assign 3 to q

            – ANG
            Nov 13 '18 at 17:46











          • Fantastic! Thank you, ANG!

            – Geet
            Nov 13 '18 at 19:06
















          1














          Try out:



          library(tidyverse)
          df <- tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4,
          15L, 15L, 15L, 15L, 15, 15L,
          17L, 2L, 16L, 30L, 34, 54L,
          29L, 2L, 16L, 30L, 34, 54L,
          25L, 2L, 17L, 20L, 26, 43L)

          df %>%
          mutate(q = ifelse(val > q1 & val < q2, 2,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, 4,
          3)),
          diff = ifelse(val > q1 & val < q2, q2 - val,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, q4 - val,
          q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <dbl>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1


          With case_when (assuming that when val is between q2 and q3, you pick 3).



          df %>%
          mutate(q = case_when(val > q1 & val < q2 ~ 2,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ 4,
          val > q2 & val < q3 ~ 3),
          diff = case_when(val > q1 & val < q2 ~ q2 - val,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ q4 - val,
          val > q2 & val < q3 ~ as.integer(q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <int>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1





          share|improve this answer





















          • 1





            I guess, case_when will also work!

            – Geet
            Nov 13 '18 at 17:42











          • Of course, but you should first specify a clear condition to assign 3 to q

            – ANG
            Nov 13 '18 at 17:46











          • Fantastic! Thank you, ANG!

            – Geet
            Nov 13 '18 at 19:06














          1












          1








          1







          Try out:



          library(tidyverse)
          df <- tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4,
          15L, 15L, 15L, 15L, 15, 15L,
          17L, 2L, 16L, 30L, 34, 54L,
          29L, 2L, 16L, 30L, 34, 54L,
          25L, 2L, 17L, 20L, 26, 43L)

          df %>%
          mutate(q = ifelse(val > q1 & val < q2, 2,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, 4,
          3)),
          diff = ifelse(val > q1 & val < q2, q2 - val,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, q4 - val,
          q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <dbl>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1


          With case_when (assuming that when val is between q2 and q3, you pick 3).



          df %>%
          mutate(q = case_when(val > q1 & val < q2 ~ 2,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ 4,
          val > q2 & val < q3 ~ 3),
          diff = case_when(val > q1 & val < q2 ~ q2 - val,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ q4 - val,
          val > q2 & val < q3 ~ as.integer(q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <int>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1





          share|improve this answer















          Try out:



          library(tidyverse)
          df <- tribble(
          ~val, ~q0, ~q1, ~q2, ~q3, ~q4,
          15L, 15L, 15L, 15L, 15, 15L,
          17L, 2L, 16L, 30L, 34, 54L,
          29L, 2L, 16L, 30L, 34, 54L,
          25L, 2L, 17L, 20L, 26, 43L)

          df %>%
          mutate(q = ifelse(val > q1 & val < q2, 2,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, 4,
          3)),
          diff = ifelse(val > q1 & val < q2, q2 - val,
          ifelse(val == q0 & val == q1 & val == q2 & val == q3 & val == q4, q4 - val,
          q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <dbl>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1


          With case_when (assuming that when val is between q2 and q3, you pick 3).



          df %>%
          mutate(q = case_when(val > q1 & val < q2 ~ 2,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ 4,
          val > q2 & val < q3 ~ 3),
          diff = case_when(val > q1 & val < q2 ~ q2 - val,
          val == q0 & val == q1 & val == q2 & val == q3 & val == q4 ~ q4 - val,
          val > q2 & val < q3 ~ as.integer(q3 - val)))
          # A tibble: 4 x 8
          val q0 q1 q2 q3 q4 q diff
          <int> <int> <int> <int> <dbl> <int> <dbl> <int>
          1 15 15 15 15 15 15 4 0
          2 17 2 16 30 34 54 2 13
          3 29 2 16 30 34 54 2 1
          4 25 2 17 20 26 43 3 1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 18:12

























          answered Nov 13 '18 at 17:40









          ANGANG

          4,3412620




          4,3412620








          • 1





            I guess, case_when will also work!

            – Geet
            Nov 13 '18 at 17:42











          • Of course, but you should first specify a clear condition to assign 3 to q

            – ANG
            Nov 13 '18 at 17:46











          • Fantastic! Thank you, ANG!

            – Geet
            Nov 13 '18 at 19:06














          • 1





            I guess, case_when will also work!

            – Geet
            Nov 13 '18 at 17:42











          • Of course, but you should first specify a clear condition to assign 3 to q

            – ANG
            Nov 13 '18 at 17:46











          • Fantastic! Thank you, ANG!

            – Geet
            Nov 13 '18 at 19:06








          1




          1





          I guess, case_when will also work!

          – Geet
          Nov 13 '18 at 17:42





          I guess, case_when will also work!

          – Geet
          Nov 13 '18 at 17:42













          Of course, but you should first specify a clear condition to assign 3 to q

          – ANG
          Nov 13 '18 at 17:46





          Of course, but you should first specify a clear condition to assign 3 to q

          – ANG
          Nov 13 '18 at 17:46













          Fantastic! Thank you, ANG!

          – Geet
          Nov 13 '18 at 19:06





          Fantastic! Thank you, ANG!

          – Geet
          Nov 13 '18 at 19:06


















          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%2f53285617%2fdifference-in-values-between-different-columns-satisfying-a-given-condition%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

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly