Problem while using Group_by/mutate function in R












0















I am trying to calculate the variance between preceding rows using group_by and lag on the below dataframe



ID    DATE      Value
555 1/9/2018 10
555 2/9/2018 20
555 3/9/2018 50
555 4/9/2018 70
000 1/9/2018 0
000 2/9/2018 5
000 3/9/2018 15
111 1/9/2018 0
111 2/9/2018 15
111 3/9/2018 20
111 4/9/2018 25


The difference is supposed to show as follow:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 0
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 0
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5


By using this line of code



data %>% 
group_by(ID) %>%
arrange(DATE) %>%
mutate(Diff= Value - lag(Value, default = first(Value)))


It skips the grouping condition by ID and calculate the difference between all the rows like this:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 -70
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 -15
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5









share|improve this question


















  • 1





    To me your code works fine, just the order or rows is different.

    – Julius Vainora
    Nov 24 '18 at 23:52











  • Actually, your code works fine for me as well.

    – arg0naut
    Nov 24 '18 at 23:57











  • @JuliusVainora can concatenation affects the group_by? because the ID is originally concatenated from 2 columns

    – Zidane Ahmed
    Nov 25 '18 at 0:06











  • @ZidaneAhmed, if I understand correctly what you mean, concatenation alone shouldn't be a problem, but perhaps the way it was done (the order of different actions, e.g.) has an effect. I guess you should revise your example.

    – Julius Vainora
    Nov 25 '18 at 0:16











  • Your code works fine. Diff = c(0, diff(Value)) is another option where you dont need to use lag...

    – Ika8
    Nov 27 '18 at 15:07
















0















I am trying to calculate the variance between preceding rows using group_by and lag on the below dataframe



ID    DATE      Value
555 1/9/2018 10
555 2/9/2018 20
555 3/9/2018 50
555 4/9/2018 70
000 1/9/2018 0
000 2/9/2018 5
000 3/9/2018 15
111 1/9/2018 0
111 2/9/2018 15
111 3/9/2018 20
111 4/9/2018 25


The difference is supposed to show as follow:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 0
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 0
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5


By using this line of code



data %>% 
group_by(ID) %>%
arrange(DATE) %>%
mutate(Diff= Value - lag(Value, default = first(Value)))


It skips the grouping condition by ID and calculate the difference between all the rows like this:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 -70
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 -15
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5









share|improve this question


















  • 1





    To me your code works fine, just the order or rows is different.

    – Julius Vainora
    Nov 24 '18 at 23:52











  • Actually, your code works fine for me as well.

    – arg0naut
    Nov 24 '18 at 23:57











  • @JuliusVainora can concatenation affects the group_by? because the ID is originally concatenated from 2 columns

    – Zidane Ahmed
    Nov 25 '18 at 0:06











  • @ZidaneAhmed, if I understand correctly what you mean, concatenation alone shouldn't be a problem, but perhaps the way it was done (the order of different actions, e.g.) has an effect. I guess you should revise your example.

    – Julius Vainora
    Nov 25 '18 at 0:16











  • Your code works fine. Diff = c(0, diff(Value)) is another option where you dont need to use lag...

    – Ika8
    Nov 27 '18 at 15:07














0












0








0








I am trying to calculate the variance between preceding rows using group_by and lag on the below dataframe



ID    DATE      Value
555 1/9/2018 10
555 2/9/2018 20
555 3/9/2018 50
555 4/9/2018 70
000 1/9/2018 0
000 2/9/2018 5
000 3/9/2018 15
111 1/9/2018 0
111 2/9/2018 15
111 3/9/2018 20
111 4/9/2018 25


The difference is supposed to show as follow:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 0
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 0
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5


By using this line of code



data %>% 
group_by(ID) %>%
arrange(DATE) %>%
mutate(Diff= Value - lag(Value, default = first(Value)))


It skips the grouping condition by ID and calculate the difference between all the rows like this:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 -70
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 -15
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5









share|improve this question














I am trying to calculate the variance between preceding rows using group_by and lag on the below dataframe



ID    DATE      Value
555 1/9/2018 10
555 2/9/2018 20
555 3/9/2018 50
555 4/9/2018 70
000 1/9/2018 0
000 2/9/2018 5
000 3/9/2018 15
111 1/9/2018 0
111 2/9/2018 15
111 3/9/2018 20
111 4/9/2018 25


The difference is supposed to show as follow:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 0
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 0
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5


By using this line of code



data %>% 
group_by(ID) %>%
arrange(DATE) %>%
mutate(Diff= Value - lag(Value, default = first(Value)))


It skips the grouping condition by ID and calculate the difference between all the rows like this:



ID    DATE      Value    Diff
555 1/9/2018 10 0
555 2/9/2018 20 10
555 3/9/2018 50 30
555 4/9/2018 70 20
000 1/9/2018 0 -70
000 2/9/2018 5 5
000 3/9/2018 15 10
111 1/9/2018 0 -15
111 2/9/2018 15 15
111 3/9/2018 20 5
111 4/9/2018 25 5






r group-by dplyr rstudio mutate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 23:44









Zidane AhmedZidane Ahmed

112




112








  • 1





    To me your code works fine, just the order or rows is different.

    – Julius Vainora
    Nov 24 '18 at 23:52











  • Actually, your code works fine for me as well.

    – arg0naut
    Nov 24 '18 at 23:57











  • @JuliusVainora can concatenation affects the group_by? because the ID is originally concatenated from 2 columns

    – Zidane Ahmed
    Nov 25 '18 at 0:06











  • @ZidaneAhmed, if I understand correctly what you mean, concatenation alone shouldn't be a problem, but perhaps the way it was done (the order of different actions, e.g.) has an effect. I guess you should revise your example.

    – Julius Vainora
    Nov 25 '18 at 0:16











  • Your code works fine. Diff = c(0, diff(Value)) is another option where you dont need to use lag...

    – Ika8
    Nov 27 '18 at 15:07














  • 1





    To me your code works fine, just the order or rows is different.

    – Julius Vainora
    Nov 24 '18 at 23:52











  • Actually, your code works fine for me as well.

    – arg0naut
    Nov 24 '18 at 23:57











  • @JuliusVainora can concatenation affects the group_by? because the ID is originally concatenated from 2 columns

    – Zidane Ahmed
    Nov 25 '18 at 0:06











  • @ZidaneAhmed, if I understand correctly what you mean, concatenation alone shouldn't be a problem, but perhaps the way it was done (the order of different actions, e.g.) has an effect. I guess you should revise your example.

    – Julius Vainora
    Nov 25 '18 at 0:16











  • Your code works fine. Diff = c(0, diff(Value)) is another option where you dont need to use lag...

    – Ika8
    Nov 27 '18 at 15:07








1




1





To me your code works fine, just the order or rows is different.

– Julius Vainora
Nov 24 '18 at 23:52





To me your code works fine, just the order or rows is different.

– Julius Vainora
Nov 24 '18 at 23:52













Actually, your code works fine for me as well.

– arg0naut
Nov 24 '18 at 23:57





Actually, your code works fine for me as well.

– arg0naut
Nov 24 '18 at 23:57













@JuliusVainora can concatenation affects the group_by? because the ID is originally concatenated from 2 columns

– Zidane Ahmed
Nov 25 '18 at 0:06





@JuliusVainora can concatenation affects the group_by? because the ID is originally concatenated from 2 columns

– Zidane Ahmed
Nov 25 '18 at 0:06













@ZidaneAhmed, if I understand correctly what you mean, concatenation alone shouldn't be a problem, but perhaps the way it was done (the order of different actions, e.g.) has an effect. I guess you should revise your example.

– Julius Vainora
Nov 25 '18 at 0:16





@ZidaneAhmed, if I understand correctly what you mean, concatenation alone shouldn't be a problem, but perhaps the way it was done (the order of different actions, e.g.) has an effect. I guess you should revise your example.

– Julius Vainora
Nov 25 '18 at 0:16













Your code works fine. Diff = c(0, diff(Value)) is another option where you dont need to use lag...

– Ika8
Nov 27 '18 at 15:07





Your code works fine. Diff = c(0, diff(Value)) is another option where you dont need to use lag...

– Ika8
Nov 27 '18 at 15:07












1 Answer
1






active

oldest

votes


















0














Your code works for me (with a little tweak).



> data_new
# A tibble: 11 x 4
# Groups: ID [3]
ID DATE Value Diff
<chr> <fct> <int> <int>
1 555 1/9/2018 10 0
2 555 2/9/2018 20 10
3 555 3/9/2018 50 30
4 555 4/9/2018 70 20
5 000 1/9/2018 0 0
6 000 2/9/2018 5 5
7 000 3/9/2018 15 10
8 111 1/9/2018 0 0
9 111 2/9/2018 15 15
10 111 3/9/2018 20 5
11 111 4/9/2018 25 5


Code



data_new <- data %>% 
group_by(ID) %>%
mutate(Diff = Value - lag(Value, default = first(Value)))


Data



data <- structure(list(ID = c("555", "555", "555", "555", "000", "000", 
"000", "111", "111", "111", "111"), DATE = structure(c(1L, 2L,
3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L), .Label = c("1/9/2018", "2/9/2018",
"3/9/2018", "4/9/2018"), class = "factor"), Value = c(10L, 20L,
50L, 70L, 0L, 5L, 15L, 0L, 15L, 20L, 25L)), row.names = c(NA,
-11L), class = "data.frame")





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%2f53463398%2fproblem-while-using-group-by-mutate-function-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









    0














    Your code works for me (with a little tweak).



    > data_new
    # A tibble: 11 x 4
    # Groups: ID [3]
    ID DATE Value Diff
    <chr> <fct> <int> <int>
    1 555 1/9/2018 10 0
    2 555 2/9/2018 20 10
    3 555 3/9/2018 50 30
    4 555 4/9/2018 70 20
    5 000 1/9/2018 0 0
    6 000 2/9/2018 5 5
    7 000 3/9/2018 15 10
    8 111 1/9/2018 0 0
    9 111 2/9/2018 15 15
    10 111 3/9/2018 20 5
    11 111 4/9/2018 25 5


    Code



    data_new <- data %>% 
    group_by(ID) %>%
    mutate(Diff = Value - lag(Value, default = first(Value)))


    Data



    data <- structure(list(ID = c("555", "555", "555", "555", "000", "000", 
    "000", "111", "111", "111", "111"), DATE = structure(c(1L, 2L,
    3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L), .Label = c("1/9/2018", "2/9/2018",
    "3/9/2018", "4/9/2018"), class = "factor"), Value = c(10L, 20L,
    50L, 70L, 0L, 5L, 15L, 0L, 15L, 20L, 25L)), row.names = c(NA,
    -11L), class = "data.frame")





    share|improve this answer




























      0














      Your code works for me (with a little tweak).



      > data_new
      # A tibble: 11 x 4
      # Groups: ID [3]
      ID DATE Value Diff
      <chr> <fct> <int> <int>
      1 555 1/9/2018 10 0
      2 555 2/9/2018 20 10
      3 555 3/9/2018 50 30
      4 555 4/9/2018 70 20
      5 000 1/9/2018 0 0
      6 000 2/9/2018 5 5
      7 000 3/9/2018 15 10
      8 111 1/9/2018 0 0
      9 111 2/9/2018 15 15
      10 111 3/9/2018 20 5
      11 111 4/9/2018 25 5


      Code



      data_new <- data %>% 
      group_by(ID) %>%
      mutate(Diff = Value - lag(Value, default = first(Value)))


      Data



      data <- structure(list(ID = c("555", "555", "555", "555", "000", "000", 
      "000", "111", "111", "111", "111"), DATE = structure(c(1L, 2L,
      3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L), .Label = c("1/9/2018", "2/9/2018",
      "3/9/2018", "4/9/2018"), class = "factor"), Value = c(10L, 20L,
      50L, 70L, 0L, 5L, 15L, 0L, 15L, 20L, 25L)), row.names = c(NA,
      -11L), class = "data.frame")





      share|improve this answer


























        0












        0








        0







        Your code works for me (with a little tweak).



        > data_new
        # A tibble: 11 x 4
        # Groups: ID [3]
        ID DATE Value Diff
        <chr> <fct> <int> <int>
        1 555 1/9/2018 10 0
        2 555 2/9/2018 20 10
        3 555 3/9/2018 50 30
        4 555 4/9/2018 70 20
        5 000 1/9/2018 0 0
        6 000 2/9/2018 5 5
        7 000 3/9/2018 15 10
        8 111 1/9/2018 0 0
        9 111 2/9/2018 15 15
        10 111 3/9/2018 20 5
        11 111 4/9/2018 25 5


        Code



        data_new <- data %>% 
        group_by(ID) %>%
        mutate(Diff = Value - lag(Value, default = first(Value)))


        Data



        data <- structure(list(ID = c("555", "555", "555", "555", "000", "000", 
        "000", "111", "111", "111", "111"), DATE = structure(c(1L, 2L,
        3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L), .Label = c("1/9/2018", "2/9/2018",
        "3/9/2018", "4/9/2018"), class = "factor"), Value = c(10L, 20L,
        50L, 70L, 0L, 5L, 15L, 0L, 15L, 20L, 25L)), row.names = c(NA,
        -11L), class = "data.frame")





        share|improve this answer













        Your code works for me (with a little tweak).



        > data_new
        # A tibble: 11 x 4
        # Groups: ID [3]
        ID DATE Value Diff
        <chr> <fct> <int> <int>
        1 555 1/9/2018 10 0
        2 555 2/9/2018 20 10
        3 555 3/9/2018 50 30
        4 555 4/9/2018 70 20
        5 000 1/9/2018 0 0
        6 000 2/9/2018 5 5
        7 000 3/9/2018 15 10
        8 111 1/9/2018 0 0
        9 111 2/9/2018 15 15
        10 111 3/9/2018 20 5
        11 111 4/9/2018 25 5


        Code



        data_new <- data %>% 
        group_by(ID) %>%
        mutate(Diff = Value - lag(Value, default = first(Value)))


        Data



        data <- structure(list(ID = c("555", "555", "555", "555", "000", "000", 
        "000", "111", "111", "111", "111"), DATE = structure(c(1L, 2L,
        3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L), .Label = c("1/9/2018", "2/9/2018",
        "3/9/2018", "4/9/2018"), class = "factor"), Value = c(10L, 20L,
        50L, 70L, 0L, 5L, 15L, 0L, 15L, 20L, 25L)), row.names = c(NA,
        -11L), class = "data.frame")






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 23:56









        RomanRoman

        2,0791431




        2,0791431
































            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%2f53463398%2fproblem-while-using-group-by-mutate-function-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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python