calculate difference between values of a column












2














dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3),               
c1=c("w","x","y","z","w","x","y","z","w","x","y","z"),
c2=c("m","m","m","m","f","f","f","f","m","m","m","m"),
c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

> dat
s c1 c2 c3
1 1 w m 1
2 1 x m 2
3 1 y m 3
4 1 z m 4
5 2 w f 5
6 2 x f 6
7 2 y f 7
8 2 z f 8
9 3 w m 9
10 3 x m 10
11 3 y m 11
12 3 z m 12


I'd like to calculate the difference between the c3 values for every combination of c1 (w-x, x-y, etc) and for every s. The output could look like this



s  diff  c2  c3
1 w-x m -1
1 w-y m -2
1 w-z m -3
1 x-y m -1
etc


I thought the aggregate function should work but I don't know how to define the combinations and pass them to the function argument.










share|improve this question
























  • What is the significance of s and c2? Are you actually interested in diff of s&c1&c2 with other s&c1&c2s?
    – Evan Friedland
    Nov 21 '18 at 2:30
















2














dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3),               
c1=c("w","x","y","z","w","x","y","z","w","x","y","z"),
c2=c("m","m","m","m","f","f","f","f","m","m","m","m"),
c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

> dat
s c1 c2 c3
1 1 w m 1
2 1 x m 2
3 1 y m 3
4 1 z m 4
5 2 w f 5
6 2 x f 6
7 2 y f 7
8 2 z f 8
9 3 w m 9
10 3 x m 10
11 3 y m 11
12 3 z m 12


I'd like to calculate the difference between the c3 values for every combination of c1 (w-x, x-y, etc) and for every s. The output could look like this



s  diff  c2  c3
1 w-x m -1
1 w-y m -2
1 w-z m -3
1 x-y m -1
etc


I thought the aggregate function should work but I don't know how to define the combinations and pass them to the function argument.










share|improve this question
























  • What is the significance of s and c2? Are you actually interested in diff of s&c1&c2 with other s&c1&c2s?
    – Evan Friedland
    Nov 21 '18 at 2:30














2












2








2







dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3),               
c1=c("w","x","y","z","w","x","y","z","w","x","y","z"),
c2=c("m","m","m","m","f","f","f","f","m","m","m","m"),
c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

> dat
s c1 c2 c3
1 1 w m 1
2 1 x m 2
3 1 y m 3
4 1 z m 4
5 2 w f 5
6 2 x f 6
7 2 y f 7
8 2 z f 8
9 3 w m 9
10 3 x m 10
11 3 y m 11
12 3 z m 12


I'd like to calculate the difference between the c3 values for every combination of c1 (w-x, x-y, etc) and for every s. The output could look like this



s  diff  c2  c3
1 w-x m -1
1 w-y m -2
1 w-z m -3
1 x-y m -1
etc


I thought the aggregate function should work but I don't know how to define the combinations and pass them to the function argument.










share|improve this question















dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3),               
c1=c("w","x","y","z","w","x","y","z","w","x","y","z"),
c2=c("m","m","m","m","f","f","f","f","m","m","m","m"),
c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

> dat
s c1 c2 c3
1 1 w m 1
2 1 x m 2
3 1 y m 3
4 1 z m 4
5 2 w f 5
6 2 x f 6
7 2 y f 7
8 2 z f 8
9 3 w m 9
10 3 x m 10
11 3 y m 11
12 3 z m 12


I'd like to calculate the difference between the c3 values for every combination of c1 (w-x, x-y, etc) and for every s. The output could look like this



s  diff  c2  c3
1 w-x m -1
1 w-y m -2
1 w-z m -3
1 x-y m -1
etc


I thought the aggregate function should work but I don't know how to define the combinations and pass them to the function argument.







r difference






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 2:31









Ronak Shah

32.9k103753




32.9k103753










asked Nov 21 '18 at 2:11









HappyPy

1,91262135




1,91262135












  • What is the significance of s and c2? Are you actually interested in diff of s&c1&c2 with other s&c1&c2s?
    – Evan Friedland
    Nov 21 '18 at 2:30


















  • What is the significance of s and c2? Are you actually interested in diff of s&c1&c2 with other s&c1&c2s?
    – Evan Friedland
    Nov 21 '18 at 2:30
















What is the significance of s and c2? Are you actually interested in diff of s&c1&c2 with other s&c1&c2s?
– Evan Friedland
Nov 21 '18 at 2:30




What is the significance of s and c2? Are you actually interested in diff of s&c1&c2 with other s&c1&c2s?
– Evan Friedland
Nov 21 '18 at 2:30












2 Answers
2






active

oldest

votes


















2














If you are willing to use dplyr something like this should work.



dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3), c1=c("w","x","y","z","w","x","y","z","w","x","y","z"), c2=c("m","m","m","m","f","f","f","f","m","m","m","m"), c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

library(dplyr)
dat$c1 <- as.character(dat$c1)
dat2 <- dat %>%
left_join(dat, by = c("s", "c2")) %>%
filter(c1.x > c1.y) %>%
transmute(s, diff = paste(c1.y, c1.x, sep = "-"), c2, c3 = c3.y - c3.x)

dat2

## s diff c2 c3
## 1 1 w-x m -1
## 2 1 w-y m -2
## 3 1 x-y m -1
## 4 1 w-z m -3
## 5 1 x-z m -2
## 6 1 y-z m -1
## 7 2 w-x f -1
## 8 2 w-y f -2
## 9 2 x-y f -1
## 10 2 w-z f -3
## 11 2 x-z f -2
## 12 2 y-z f -1
## 13 3 w-x m -1
## 14 3 w-y m -2
## 15 3 x-y m -1
## 16 3 w-z m -3
## 17 3 x-z m -2
## 18 3 y-z m -1





share|improve this answer





























    1














    do.call(rbind, lapply(split(dat, dat$s), function(a){
    a$c1 = as.character(a$c1)
    d = setNames(data.frame(t(combn(unique(a$c1), 2, FUN = function(x){
    list(x[1], x[2], a$c3[a$c1 == x[1]] - a$c3[a$c1 == x[2]])
    }))), c("col1", "col2", "val"))
    d$s = a$s[1]
    d$c2 = a$c2[1]
    d
    }))
    # col1 col2 val s c2
    #1.1 w x -1 1 m
    #1.2 w y -2 1 m
    #1.3 w z -3 1 m
    #1.4 x y -1 1 m
    #1.5 x z -2 1 m
    #1.6 y z -1 1 m
    #2.1 w x -1 2 f
    #2.2 w y -2 2 f
    #2.3 w z -3 2 f
    #2.4 x y -1 2 f
    #2.5 x z -2 2 f
    #2.6 y z -1 2 f
    #3.1 w x -1 3 m
    #3.2 w y -2 3 m
    #3.3 w z -3 3 m
    #3.4 x y -1 3 m
    #3.5 x z -2 3 m
    #3.6 y z -1 3 m





    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',
      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%2f53404385%2fcalculate-difference-between-values-of-a-column%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














      If you are willing to use dplyr something like this should work.



      dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3), c1=c("w","x","y","z","w","x","y","z","w","x","y","z"), c2=c("m","m","m","m","f","f","f","f","m","m","m","m"), c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

      library(dplyr)
      dat$c1 <- as.character(dat$c1)
      dat2 <- dat %>%
      left_join(dat, by = c("s", "c2")) %>%
      filter(c1.x > c1.y) %>%
      transmute(s, diff = paste(c1.y, c1.x, sep = "-"), c2, c3 = c3.y - c3.x)

      dat2

      ## s diff c2 c3
      ## 1 1 w-x m -1
      ## 2 1 w-y m -2
      ## 3 1 x-y m -1
      ## 4 1 w-z m -3
      ## 5 1 x-z m -2
      ## 6 1 y-z m -1
      ## 7 2 w-x f -1
      ## 8 2 w-y f -2
      ## 9 2 x-y f -1
      ## 10 2 w-z f -3
      ## 11 2 x-z f -2
      ## 12 2 y-z f -1
      ## 13 3 w-x m -1
      ## 14 3 w-y m -2
      ## 15 3 x-y m -1
      ## 16 3 w-z m -3
      ## 17 3 x-z m -2
      ## 18 3 y-z m -1





      share|improve this answer


























        2














        If you are willing to use dplyr something like this should work.



        dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3), c1=c("w","x","y","z","w","x","y","z","w","x","y","z"), c2=c("m","m","m","m","f","f","f","f","m","m","m","m"), c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

        library(dplyr)
        dat$c1 <- as.character(dat$c1)
        dat2 <- dat %>%
        left_join(dat, by = c("s", "c2")) %>%
        filter(c1.x > c1.y) %>%
        transmute(s, diff = paste(c1.y, c1.x, sep = "-"), c2, c3 = c3.y - c3.x)

        dat2

        ## s diff c2 c3
        ## 1 1 w-x m -1
        ## 2 1 w-y m -2
        ## 3 1 x-y m -1
        ## 4 1 w-z m -3
        ## 5 1 x-z m -2
        ## 6 1 y-z m -1
        ## 7 2 w-x f -1
        ## 8 2 w-y f -2
        ## 9 2 x-y f -1
        ## 10 2 w-z f -3
        ## 11 2 x-z f -2
        ## 12 2 y-z f -1
        ## 13 3 w-x m -1
        ## 14 3 w-y m -2
        ## 15 3 x-y m -1
        ## 16 3 w-z m -3
        ## 17 3 x-z m -2
        ## 18 3 y-z m -1





        share|improve this answer
























          2












          2








          2






          If you are willing to use dplyr something like this should work.



          dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3), c1=c("w","x","y","z","w","x","y","z","w","x","y","z"), c2=c("m","m","m","m","f","f","f","f","m","m","m","m"), c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

          library(dplyr)
          dat$c1 <- as.character(dat$c1)
          dat2 <- dat %>%
          left_join(dat, by = c("s", "c2")) %>%
          filter(c1.x > c1.y) %>%
          transmute(s, diff = paste(c1.y, c1.x, sep = "-"), c2, c3 = c3.y - c3.x)

          dat2

          ## s diff c2 c3
          ## 1 1 w-x m -1
          ## 2 1 w-y m -2
          ## 3 1 x-y m -1
          ## 4 1 w-z m -3
          ## 5 1 x-z m -2
          ## 6 1 y-z m -1
          ## 7 2 w-x f -1
          ## 8 2 w-y f -2
          ## 9 2 x-y f -1
          ## 10 2 w-z f -3
          ## 11 2 x-z f -2
          ## 12 2 y-z f -1
          ## 13 3 w-x m -1
          ## 14 3 w-y m -2
          ## 15 3 x-y m -1
          ## 16 3 w-z m -3
          ## 17 3 x-z m -2
          ## 18 3 y-z m -1





          share|improve this answer












          If you are willing to use dplyr something like this should work.



          dat <- data.frame(s=c(1,1,1,1,2,2,2,2,3,3,3,3), c1=c("w","x","y","z","w","x","y","z","w","x","y","z"), c2=c("m","m","m","m","f","f","f","f","m","m","m","m"), c3=c(1,2,3,4,5,6,7,8,9,10,11,12))

          library(dplyr)
          dat$c1 <- as.character(dat$c1)
          dat2 <- dat %>%
          left_join(dat, by = c("s", "c2")) %>%
          filter(c1.x > c1.y) %>%
          transmute(s, diff = paste(c1.y, c1.x, sep = "-"), c2, c3 = c3.y - c3.x)

          dat2

          ## s diff c2 c3
          ## 1 1 w-x m -1
          ## 2 1 w-y m -2
          ## 3 1 x-y m -1
          ## 4 1 w-z m -3
          ## 5 1 x-z m -2
          ## 6 1 y-z m -1
          ## 7 2 w-x f -1
          ## 8 2 w-y f -2
          ## 9 2 x-y f -1
          ## 10 2 w-z f -3
          ## 11 2 x-z f -2
          ## 12 2 y-z f -1
          ## 13 3 w-x m -1
          ## 14 3 w-y m -2
          ## 15 3 x-y m -1
          ## 16 3 w-z m -3
          ## 17 3 x-z m -2
          ## 18 3 y-z m -1






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 2:39









          jmuhlenkamp

          1,403525




          1,403525

























              1














              do.call(rbind, lapply(split(dat, dat$s), function(a){
              a$c1 = as.character(a$c1)
              d = setNames(data.frame(t(combn(unique(a$c1), 2, FUN = function(x){
              list(x[1], x[2], a$c3[a$c1 == x[1]] - a$c3[a$c1 == x[2]])
              }))), c("col1", "col2", "val"))
              d$s = a$s[1]
              d$c2 = a$c2[1]
              d
              }))
              # col1 col2 val s c2
              #1.1 w x -1 1 m
              #1.2 w y -2 1 m
              #1.3 w z -3 1 m
              #1.4 x y -1 1 m
              #1.5 x z -2 1 m
              #1.6 y z -1 1 m
              #2.1 w x -1 2 f
              #2.2 w y -2 2 f
              #2.3 w z -3 2 f
              #2.4 x y -1 2 f
              #2.5 x z -2 2 f
              #2.6 y z -1 2 f
              #3.1 w x -1 3 m
              #3.2 w y -2 3 m
              #3.3 w z -3 3 m
              #3.4 x y -1 3 m
              #3.5 x z -2 3 m
              #3.6 y z -1 3 m





              share|improve this answer


























                1














                do.call(rbind, lapply(split(dat, dat$s), function(a){
                a$c1 = as.character(a$c1)
                d = setNames(data.frame(t(combn(unique(a$c1), 2, FUN = function(x){
                list(x[1], x[2], a$c3[a$c1 == x[1]] - a$c3[a$c1 == x[2]])
                }))), c("col1", "col2", "val"))
                d$s = a$s[1]
                d$c2 = a$c2[1]
                d
                }))
                # col1 col2 val s c2
                #1.1 w x -1 1 m
                #1.2 w y -2 1 m
                #1.3 w z -3 1 m
                #1.4 x y -1 1 m
                #1.5 x z -2 1 m
                #1.6 y z -1 1 m
                #2.1 w x -1 2 f
                #2.2 w y -2 2 f
                #2.3 w z -3 2 f
                #2.4 x y -1 2 f
                #2.5 x z -2 2 f
                #2.6 y z -1 2 f
                #3.1 w x -1 3 m
                #3.2 w y -2 3 m
                #3.3 w z -3 3 m
                #3.4 x y -1 3 m
                #3.5 x z -2 3 m
                #3.6 y z -1 3 m





                share|improve this answer
























                  1












                  1








                  1






                  do.call(rbind, lapply(split(dat, dat$s), function(a){
                  a$c1 = as.character(a$c1)
                  d = setNames(data.frame(t(combn(unique(a$c1), 2, FUN = function(x){
                  list(x[1], x[2], a$c3[a$c1 == x[1]] - a$c3[a$c1 == x[2]])
                  }))), c("col1", "col2", "val"))
                  d$s = a$s[1]
                  d$c2 = a$c2[1]
                  d
                  }))
                  # col1 col2 val s c2
                  #1.1 w x -1 1 m
                  #1.2 w y -2 1 m
                  #1.3 w z -3 1 m
                  #1.4 x y -1 1 m
                  #1.5 x z -2 1 m
                  #1.6 y z -1 1 m
                  #2.1 w x -1 2 f
                  #2.2 w y -2 2 f
                  #2.3 w z -3 2 f
                  #2.4 x y -1 2 f
                  #2.5 x z -2 2 f
                  #2.6 y z -1 2 f
                  #3.1 w x -1 3 m
                  #3.2 w y -2 3 m
                  #3.3 w z -3 3 m
                  #3.4 x y -1 3 m
                  #3.5 x z -2 3 m
                  #3.6 y z -1 3 m





                  share|improve this answer












                  do.call(rbind, lapply(split(dat, dat$s), function(a){
                  a$c1 = as.character(a$c1)
                  d = setNames(data.frame(t(combn(unique(a$c1), 2, FUN = function(x){
                  list(x[1], x[2], a$c3[a$c1 == x[1]] - a$c3[a$c1 == x[2]])
                  }))), c("col1", "col2", "val"))
                  d$s = a$s[1]
                  d$c2 = a$c2[1]
                  d
                  }))
                  # col1 col2 val s c2
                  #1.1 w x -1 1 m
                  #1.2 w y -2 1 m
                  #1.3 w z -3 1 m
                  #1.4 x y -1 1 m
                  #1.5 x z -2 1 m
                  #1.6 y z -1 1 m
                  #2.1 w x -1 2 f
                  #2.2 w y -2 2 f
                  #2.3 w z -3 2 f
                  #2.4 x y -1 2 f
                  #2.5 x z -2 2 f
                  #2.6 y z -1 2 f
                  #3.1 w x -1 3 m
                  #3.2 w y -2 3 m
                  #3.3 w z -3 3 m
                  #3.4 x y -1 3 m
                  #3.5 x z -2 3 m
                  #3.6 y z -1 3 m






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 3:18









                  d.b

                  18.5k41846




                  18.5k41846






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53404385%2fcalculate-difference-between-values-of-a-column%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga