Issue when knitting R Markdown File - Variables not recognised









up vote
0
down vote

favorite












I'm having issues referencing the output of a group by statement in R when knitting a markdown file. When I refer the variable name of the output of a group_by and summarise statement, I get an error saying the variable doesn't exist.



Below is a version of the code that works when run in R Studio but fails in R Markdown.



DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


breakdown <- DF1 %>%
group_by(name, s_id) %>%
summarise(count = n_distinct(t_id))


breakdown_v2 <- mutate(.data = breakdown,
number_of_trips = ifelse (s_id == 'ab', (count*5),
ifelse (s_id == 'cd', (count*2), (count*1))))


Something similar happened to me previously, which led me to explicitly stating the s_id in the summarise statement, but it won't work for me this time.



Any idea?
Thanks



Update: Actual Code Used:



```r Busiest/Quietest Routes
# I needed to find the number of distinct trips per service before multiplying out the trips per week.

distinct_trips_breakdown <- Overall_Dublin_Bus_Record %>%
group_by(route_short_name, service_id) %>%
summarise(count = n_distinct(trip_id))

distinct_trips_breakdown <- mutate(.data = distinct_trips_breakdown,
number_of_trips_per_week = ifelse (service_id == '1', (count*5), ifelse (service_id == '2', (count*2), (count*1))))

Overall_trips_per_week <- distinct_trips_breakdown %>%
group_by(route_short_name) %>%
summarise(total_trips_per_week = sum(number_of_trips_per_week))

Busiest_Routes <- top_n(Overall_trips_per_week, 5)
Quiestest_Routes <- top_n(Overall_trips_per_week, -5)

```









share|improve this question



























    up vote
    0
    down vote

    favorite












    I'm having issues referencing the output of a group by statement in R when knitting a markdown file. When I refer the variable name of the output of a group_by and summarise statement, I get an error saying the variable doesn't exist.



    Below is a version of the code that works when run in R Studio but fails in R Markdown.



    DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
    s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
    t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


    breakdown <- DF1 %>%
    group_by(name, s_id) %>%
    summarise(count = n_distinct(t_id))


    breakdown_v2 <- mutate(.data = breakdown,
    number_of_trips = ifelse (s_id == 'ab', (count*5),
    ifelse (s_id == 'cd', (count*2), (count*1))))


    Something similar happened to me previously, which led me to explicitly stating the s_id in the summarise statement, but it won't work for me this time.



    Any idea?
    Thanks



    Update: Actual Code Used:



    ```r Busiest/Quietest Routes
    # I needed to find the number of distinct trips per service before multiplying out the trips per week.

    distinct_trips_breakdown <- Overall_Dublin_Bus_Record %>%
    group_by(route_short_name, service_id) %>%
    summarise(count = n_distinct(trip_id))

    distinct_trips_breakdown <- mutate(.data = distinct_trips_breakdown,
    number_of_trips_per_week = ifelse (service_id == '1', (count*5), ifelse (service_id == '2', (count*2), (count*1))))

    Overall_trips_per_week <- distinct_trips_breakdown %>%
    group_by(route_short_name) %>%
    summarise(total_trips_per_week = sum(number_of_trips_per_week))

    Busiest_Routes <- top_n(Overall_trips_per_week, 5)
    Quiestest_Routes <- top_n(Overall_trips_per_week, -5)

    ```









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm having issues referencing the output of a group by statement in R when knitting a markdown file. When I refer the variable name of the output of a group_by and summarise statement, I get an error saying the variable doesn't exist.



      Below is a version of the code that works when run in R Studio but fails in R Markdown.



      DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
      s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
      t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


      breakdown <- DF1 %>%
      group_by(name, s_id) %>%
      summarise(count = n_distinct(t_id))


      breakdown_v2 <- mutate(.data = breakdown,
      number_of_trips = ifelse (s_id == 'ab', (count*5),
      ifelse (s_id == 'cd', (count*2), (count*1))))


      Something similar happened to me previously, which led me to explicitly stating the s_id in the summarise statement, but it won't work for me this time.



      Any idea?
      Thanks



      Update: Actual Code Used:



      ```r Busiest/Quietest Routes
      # I needed to find the number of distinct trips per service before multiplying out the trips per week.

      distinct_trips_breakdown <- Overall_Dublin_Bus_Record %>%
      group_by(route_short_name, service_id) %>%
      summarise(count = n_distinct(trip_id))

      distinct_trips_breakdown <- mutate(.data = distinct_trips_breakdown,
      number_of_trips_per_week = ifelse (service_id == '1', (count*5), ifelse (service_id == '2', (count*2), (count*1))))

      Overall_trips_per_week <- distinct_trips_breakdown %>%
      group_by(route_short_name) %>%
      summarise(total_trips_per_week = sum(number_of_trips_per_week))

      Busiest_Routes <- top_n(Overall_trips_per_week, 5)
      Quiestest_Routes <- top_n(Overall_trips_per_week, -5)

      ```









      share|improve this question















      I'm having issues referencing the output of a group by statement in R when knitting a markdown file. When I refer the variable name of the output of a group_by and summarise statement, I get an error saying the variable doesn't exist.



      Below is a version of the code that works when run in R Studio but fails in R Markdown.



      DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
      s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
      t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


      breakdown <- DF1 %>%
      group_by(name, s_id) %>%
      summarise(count = n_distinct(t_id))


      breakdown_v2 <- mutate(.data = breakdown,
      number_of_trips = ifelse (s_id == 'ab', (count*5),
      ifelse (s_id == 'cd', (count*2), (count*1))))


      Something similar happened to me previously, which led me to explicitly stating the s_id in the summarise statement, but it won't work for me this time.



      Any idea?
      Thanks



      Update: Actual Code Used:



      ```r Busiest/Quietest Routes
      # I needed to find the number of distinct trips per service before multiplying out the trips per week.

      distinct_trips_breakdown <- Overall_Dublin_Bus_Record %>%
      group_by(route_short_name, service_id) %>%
      summarise(count = n_distinct(trip_id))

      distinct_trips_breakdown <- mutate(.data = distinct_trips_breakdown,
      number_of_trips_per_week = ifelse (service_id == '1', (count*5), ifelse (service_id == '2', (count*2), (count*1))))

      Overall_trips_per_week <- distinct_trips_breakdown %>%
      group_by(route_short_name) %>%
      summarise(total_trips_per_week = sum(number_of_trips_per_week))

      Busiest_Routes <- top_n(Overall_trips_per_week, 5)
      Quiestest_Routes <- top_n(Overall_trips_per_week, -5)

      ```






      r r-markdown






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 21:58

























      asked Nov 11 at 21:27









      Bake.G

      73119




      73119






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          For me the code works fine. Could you write exactly your code chunk ? It might be a problem of spaces or that you did not put the 3 ``` to end the chunk down. Also, remember to load the packages. Try to use the following chunk, which works for me (having loaded the packages):



          ```r 

          DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
          s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
          t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


          breakdown <- DF1 %>%
          group_by(name, s_id) %>%
          summarise(count = n_distinct(t_id))


          breakdown_v2 <- mutate(.data = breakdown,
          number_of_trips = ifelse (s_id == 'ab', (count*5),
          ifelse (s_id == 'cd', (count*2), (count*1))))
          ```


          `



          Cheers!






          share|improve this answer






















          • Hey dude, I just updated my question. Could the issue lie when you paste the normal code from R Studio into a markdown type layout?
            – Bake.G
            Nov 11 at 22:00







          • 1




            I cannot reproduce the second part of the code. As far as I know, I have just tried to copy paste other code into Rmarkdown from RStudio and I am not having that problem. Cheers!
            – Carles Sans Fuentes
            Nov 11 at 22:08

















          up vote
          0
          down vote



          accepted










          I fixed this error in the end. The issue was not when loading up plyr before dplyr...Solved it by putting dplyr:: before summarise.



          ```r 

          DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
          s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
          t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


          breakdown <- DF1 %>%
          group_by(name, s_id) %>%
          dplyr::summarise(count = n_distinct(t_id))


          breakdown_v2 <- mutate(.data = breakdown,
          number_of_trips = ifelse (s_id == 'ab', (count*5),
          ifelse (s_id == 'cd', (count*2), (count*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%2f53253401%2fissue-when-knitting-r-markdown-file-variables-not-recognised%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








            up vote
            0
            down vote













            For me the code works fine. Could you write exactly your code chunk ? It might be a problem of spaces or that you did not put the 3 ``` to end the chunk down. Also, remember to load the packages. Try to use the following chunk, which works for me (having loaded the packages):



            ```r 

            DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
            s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
            t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


            breakdown <- DF1 %>%
            group_by(name, s_id) %>%
            summarise(count = n_distinct(t_id))


            breakdown_v2 <- mutate(.data = breakdown,
            number_of_trips = ifelse (s_id == 'ab', (count*5),
            ifelse (s_id == 'cd', (count*2), (count*1))))
            ```


            `



            Cheers!






            share|improve this answer






















            • Hey dude, I just updated my question. Could the issue lie when you paste the normal code from R Studio into a markdown type layout?
              – Bake.G
              Nov 11 at 22:00







            • 1




              I cannot reproduce the second part of the code. As far as I know, I have just tried to copy paste other code into Rmarkdown from RStudio and I am not having that problem. Cheers!
              – Carles Sans Fuentes
              Nov 11 at 22:08














            up vote
            0
            down vote













            For me the code works fine. Could you write exactly your code chunk ? It might be a problem of spaces or that you did not put the 3 ``` to end the chunk down. Also, remember to load the packages. Try to use the following chunk, which works for me (having loaded the packages):



            ```r 

            DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
            s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
            t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


            breakdown <- DF1 %>%
            group_by(name, s_id) %>%
            summarise(count = n_distinct(t_id))


            breakdown_v2 <- mutate(.data = breakdown,
            number_of_trips = ifelse (s_id == 'ab', (count*5),
            ifelse (s_id == 'cd', (count*2), (count*1))))
            ```


            `



            Cheers!






            share|improve this answer






















            • Hey dude, I just updated my question. Could the issue lie when you paste the normal code from R Studio into a markdown type layout?
              – Bake.G
              Nov 11 at 22:00







            • 1




              I cannot reproduce the second part of the code. As far as I know, I have just tried to copy paste other code into Rmarkdown from RStudio and I am not having that problem. Cheers!
              – Carles Sans Fuentes
              Nov 11 at 22:08












            up vote
            0
            down vote










            up vote
            0
            down vote









            For me the code works fine. Could you write exactly your code chunk ? It might be a problem of spaces or that you did not put the 3 ``` to end the chunk down. Also, remember to load the packages. Try to use the following chunk, which works for me (having loaded the packages):



            ```r 

            DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
            s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
            t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


            breakdown <- DF1 %>%
            group_by(name, s_id) %>%
            summarise(count = n_distinct(t_id))


            breakdown_v2 <- mutate(.data = breakdown,
            number_of_trips = ifelse (s_id == 'ab', (count*5),
            ifelse (s_id == 'cd', (count*2), (count*1))))
            ```


            `



            Cheers!






            share|improve this answer














            For me the code works fine. Could you write exactly your code chunk ? It might be a problem of spaces or that you did not put the 3 ``` to end the chunk down. Also, remember to load the packages. Try to use the following chunk, which works for me (having loaded the packages):



            ```r 

            DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
            s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
            t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


            breakdown <- DF1 %>%
            group_by(name, s_id) %>%
            summarise(count = n_distinct(t_id))


            breakdown_v2 <- mutate(.data = breakdown,
            number_of_trips = ifelse (s_id == 'ab', (count*5),
            ifelse (s_id == 'cd', (count*2), (count*1))))
            ```


            `



            Cheers!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 21:56

























            answered Nov 11 at 21:35









            Carles Sans Fuentes

            51918




            51918











            • Hey dude, I just updated my question. Could the issue lie when you paste the normal code from R Studio into a markdown type layout?
              – Bake.G
              Nov 11 at 22:00







            • 1




              I cannot reproduce the second part of the code. As far as I know, I have just tried to copy paste other code into Rmarkdown from RStudio and I am not having that problem. Cheers!
              – Carles Sans Fuentes
              Nov 11 at 22:08
















            • Hey dude, I just updated my question. Could the issue lie when you paste the normal code from R Studio into a markdown type layout?
              – Bake.G
              Nov 11 at 22:00







            • 1




              I cannot reproduce the second part of the code. As far as I know, I have just tried to copy paste other code into Rmarkdown from RStudio and I am not having that problem. Cheers!
              – Carles Sans Fuentes
              Nov 11 at 22:08















            Hey dude, I just updated my question. Could the issue lie when you paste the normal code from R Studio into a markdown type layout?
            – Bake.G
            Nov 11 at 22:00





            Hey dude, I just updated my question. Could the issue lie when you paste the normal code from R Studio into a markdown type layout?
            – Bake.G
            Nov 11 at 22:00





            1




            1




            I cannot reproduce the second part of the code. As far as I know, I have just tried to copy paste other code into Rmarkdown from RStudio and I am not having that problem. Cheers!
            – Carles Sans Fuentes
            Nov 11 at 22:08




            I cannot reproduce the second part of the code. As far as I know, I have just tried to copy paste other code into Rmarkdown from RStudio and I am not having that problem. Cheers!
            – Carles Sans Fuentes
            Nov 11 at 22:08












            up vote
            0
            down vote



            accepted










            I fixed this error in the end. The issue was not when loading up plyr before dplyr...Solved it by putting dplyr:: before summarise.



            ```r 

            DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
            s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
            t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


            breakdown <- DF1 %>%
            group_by(name, s_id) %>%
            dplyr::summarise(count = n_distinct(t_id))


            breakdown_v2 <- mutate(.data = breakdown,
            number_of_trips = ifelse (s_id == 'ab', (count*5),
            ifelse (s_id == 'cd', (count*2), (count*1))))
            ```





            share|improve this answer
























              up vote
              0
              down vote



              accepted










              I fixed this error in the end. The issue was not when loading up plyr before dplyr...Solved it by putting dplyr:: before summarise.



              ```r 

              DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
              s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
              t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


              breakdown <- DF1 %>%
              group_by(name, s_id) %>%
              dplyr::summarise(count = n_distinct(t_id))


              breakdown_v2 <- mutate(.data = breakdown,
              number_of_trips = ifelse (s_id == 'ab', (count*5),
              ifelse (s_id == 'cd', (count*2), (count*1))))
              ```





              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                I fixed this error in the end. The issue was not when loading up plyr before dplyr...Solved it by putting dplyr:: before summarise.



                ```r 

                DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
                s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
                t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


                breakdown <- DF1 %>%
                group_by(name, s_id) %>%
                dplyr::summarise(count = n_distinct(t_id))


                breakdown_v2 <- mutate(.data = breakdown,
                number_of_trips = ifelse (s_id == 'ab', (count*5),
                ifelse (s_id == 'cd', (count*2), (count*1))))
                ```





                share|improve this answer












                I fixed this error in the end. The issue was not when loading up plyr before dplyr...Solved it by putting dplyr:: before summarise.



                ```r 

                DF1 <- data.frame(name = c("1", "1", "2", "2", "3", "1" ),
                s_id = c("ab", "ab", "cd", "ab", " bc", "ab"),
                t_id = c("12A", "12A", "12A", "14B", "14B", "14B"))


                breakdown <- DF1 %>%
                group_by(name, s_id) %>%
                dplyr::summarise(count = n_distinct(t_id))


                breakdown_v2 <- mutate(.data = breakdown,
                number_of_trips = ifelse (s_id == 'ab', (count*5),
                ifelse (s_id == 'cd', (count*2), (count*1))))
                ```






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 3 at 22:59









                Bake.G

                73119




                73119



























                    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%2f53253401%2fissue-when-knitting-r-markdown-file-variables-not-recognised%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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線