Piping into rcorr









up vote
2
down vote

favorite












I am trying to run rcorr as part of a function over multiple dataframes, extracting p-values for each test but am receiving an NA values when piping into rcorr.



For example if I create a matrix and run rcorr on this matrix, extracting the pvalue table with $P and the pvalue with [2] it works...



library(Hmisc)
library(magrittr)

mt <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), ncol=2)
rcorr(mt, type="pearson")$P[2]
[1] 0


But if I try and pipe this I only recieve NAs.



mt %>% rcorr(., type="pearson")$P[2]
[1] NA NA

mt %>% rcorr(., type="pearson")$P
Error in .$rcorr(., type = "pearson") :
3 arguments passed to '$' which requires 2


Can someone explain to me why this doesnt work or give a workaround? Ideally I don't want to have to create variables for each of my matrices before running rcorr



Thanks in advance.










share|improve this question



















  • 1




    It would be helpful to add the statement library(Hmisc) to the code in your question since rcorr is not part of base R.
    – G5W
    Nov 9 at 21:42















up vote
2
down vote

favorite












I am trying to run rcorr as part of a function over multiple dataframes, extracting p-values for each test but am receiving an NA values when piping into rcorr.



For example if I create a matrix and run rcorr on this matrix, extracting the pvalue table with $P and the pvalue with [2] it works...



library(Hmisc)
library(magrittr)

mt <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), ncol=2)
rcorr(mt, type="pearson")$P[2]
[1] 0


But if I try and pipe this I only recieve NAs.



mt %>% rcorr(., type="pearson")$P[2]
[1] NA NA

mt %>% rcorr(., type="pearson")$P
Error in .$rcorr(., type = "pearson") :
3 arguments passed to '$' which requires 2


Can someone explain to me why this doesnt work or give a workaround? Ideally I don't want to have to create variables for each of my matrices before running rcorr



Thanks in advance.










share|improve this question



















  • 1




    It would be helpful to add the statement library(Hmisc) to the code in your question since rcorr is not part of base R.
    – G5W
    Nov 9 at 21:42













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am trying to run rcorr as part of a function over multiple dataframes, extracting p-values for each test but am receiving an NA values when piping into rcorr.



For example if I create a matrix and run rcorr on this matrix, extracting the pvalue table with $P and the pvalue with [2] it works...



library(Hmisc)
library(magrittr)

mt <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), ncol=2)
rcorr(mt, type="pearson")$P[2]
[1] 0


But if I try and pipe this I only recieve NAs.



mt %>% rcorr(., type="pearson")$P[2]
[1] NA NA

mt %>% rcorr(., type="pearson")$P
Error in .$rcorr(., type = "pearson") :
3 arguments passed to '$' which requires 2


Can someone explain to me why this doesnt work or give a workaround? Ideally I don't want to have to create variables for each of my matrices before running rcorr



Thanks in advance.










share|improve this question















I am trying to run rcorr as part of a function over multiple dataframes, extracting p-values for each test but am receiving an NA values when piping into rcorr.



For example if I create a matrix and run rcorr on this matrix, extracting the pvalue table with $P and the pvalue with [2] it works...



library(Hmisc)
library(magrittr)

mt <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), ncol=2)
rcorr(mt, type="pearson")$P[2]
[1] 0


But if I try and pipe this I only recieve NAs.



mt %>% rcorr(., type="pearson")$P[2]
[1] NA NA

mt %>% rcorr(., type="pearson")$P
Error in .$rcorr(., type = "pearson") :
3 arguments passed to '$' which requires 2


Can someone explain to me why this doesnt work or give a workaround? Ideally I don't want to have to create variables for each of my matrices before running rcorr



Thanks in advance.







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Florian

707616




707616










asked Nov 9 at 21:02









EllieFev

537




537







  • 1




    It would be helpful to add the statement library(Hmisc) to the code in your question since rcorr is not part of base R.
    – G5W
    Nov 9 at 21:42













  • 1




    It would be helpful to add the statement library(Hmisc) to the code in your question since rcorr is not part of base R.
    – G5W
    Nov 9 at 21:42








1




1




It would be helpful to add the statement library(Hmisc) to the code in your question since rcorr is not part of base R.
– G5W
Nov 9 at 21:42





It would be helpful to add the statement library(Hmisc) to the code in your question since rcorr is not part of base R.
– G5W
Nov 9 at 21:42













3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Solution



(mt %>% mcor(type = "pearson"))$P[2]
# [1] 0


Explanation



Notice that both



mt %>% rcorr(., type = "pearson")


and



mt %>% rcorr(type = "pearson")


work as expected. The problem is that you add $ and [ to the second object, which basically are like subsequent function calls. For instance,



s <- function(x) c(1, 1 + x)
1 %>% s
# [1] 1 2


works as expected, but



1 %>% s[1]
# Error in .[s, 1] : incorrect number of dimensions


doesn't return 1 since we are trying to do something like s[1](1) instead.



Now



1 %>% s(x = .)[1]
# Error in .[s(x = .), 1] : incorrect number of dimensions


just as yours



mt %>% rcorr(., type = "pearson")$P[2]
# [1] NA NA


is trickier. Notice that it can be rewritten as



mt %>% `[`(`$`(rcorr(., type = "pearson"), "P"), 2)
# [1] NA NA


So, now it becomes clear that the latter doesn't work because it basically is



`[`(mt, `$`(rcorr(mt, type = "pearson"), "P"), 2)
# [1] NA NA


which, when deciphered, is



mt[rcorr(mt, type = "pearson")$P, 2]
# [1] NA NA





share|improve this answer





























    up vote
    1
    down vote













    A tidy solution, at least I hope!



    library(dplyr) 
    library(broom)
    library(Hmisc)

    mtcars[, 5:6] %>%
    as.matrix()%>%
    rcorr()%>%
    tidy() %>%
    select(estimate)





    share|improve this answer





























      up vote
      0
      down vote













      A simple solution using %$% from magrittr:



      library(Hmisc)
      library(magrittr)
      mt <- matrix(1:10, ncol=2)

      mt %>% rcorr(type="pearson") %$% P[2]
      [1] 0





      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%2f53233231%2fpiping-into-rcorr%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        Solution



        (mt %>% mcor(type = "pearson"))$P[2]
        # [1] 0


        Explanation



        Notice that both



        mt %>% rcorr(., type = "pearson")


        and



        mt %>% rcorr(type = "pearson")


        work as expected. The problem is that you add $ and [ to the second object, which basically are like subsequent function calls. For instance,



        s <- function(x) c(1, 1 + x)
        1 %>% s
        # [1] 1 2


        works as expected, but



        1 %>% s[1]
        # Error in .[s, 1] : incorrect number of dimensions


        doesn't return 1 since we are trying to do something like s[1](1) instead.



        Now



        1 %>% s(x = .)[1]
        # Error in .[s(x = .), 1] : incorrect number of dimensions


        just as yours



        mt %>% rcorr(., type = "pearson")$P[2]
        # [1] NA NA


        is trickier. Notice that it can be rewritten as



        mt %>% `[`(`$`(rcorr(., type = "pearson"), "P"), 2)
        # [1] NA NA


        So, now it becomes clear that the latter doesn't work because it basically is



        `[`(mt, `$`(rcorr(mt, type = "pearson"), "P"), 2)
        # [1] NA NA


        which, when deciphered, is



        mt[rcorr(mt, type = "pearson")$P, 2]
        # [1] NA NA





        share|improve this answer


























          up vote
          2
          down vote



          accepted










          Solution



          (mt %>% mcor(type = "pearson"))$P[2]
          # [1] 0


          Explanation



          Notice that both



          mt %>% rcorr(., type = "pearson")


          and



          mt %>% rcorr(type = "pearson")


          work as expected. The problem is that you add $ and [ to the second object, which basically are like subsequent function calls. For instance,



          s <- function(x) c(1, 1 + x)
          1 %>% s
          # [1] 1 2


          works as expected, but



          1 %>% s[1]
          # Error in .[s, 1] : incorrect number of dimensions


          doesn't return 1 since we are trying to do something like s[1](1) instead.



          Now



          1 %>% s(x = .)[1]
          # Error in .[s(x = .), 1] : incorrect number of dimensions


          just as yours



          mt %>% rcorr(., type = "pearson")$P[2]
          # [1] NA NA


          is trickier. Notice that it can be rewritten as



          mt %>% `[`(`$`(rcorr(., type = "pearson"), "P"), 2)
          # [1] NA NA


          So, now it becomes clear that the latter doesn't work because it basically is



          `[`(mt, `$`(rcorr(mt, type = "pearson"), "P"), 2)
          # [1] NA NA


          which, when deciphered, is



          mt[rcorr(mt, type = "pearson")$P, 2]
          # [1] NA NA





          share|improve this answer
























            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Solution



            (mt %>% mcor(type = "pearson"))$P[2]
            # [1] 0


            Explanation



            Notice that both



            mt %>% rcorr(., type = "pearson")


            and



            mt %>% rcorr(type = "pearson")


            work as expected. The problem is that you add $ and [ to the second object, which basically are like subsequent function calls. For instance,



            s <- function(x) c(1, 1 + x)
            1 %>% s
            # [1] 1 2


            works as expected, but



            1 %>% s[1]
            # Error in .[s, 1] : incorrect number of dimensions


            doesn't return 1 since we are trying to do something like s[1](1) instead.



            Now



            1 %>% s(x = .)[1]
            # Error in .[s(x = .), 1] : incorrect number of dimensions


            just as yours



            mt %>% rcorr(., type = "pearson")$P[2]
            # [1] NA NA


            is trickier. Notice that it can be rewritten as



            mt %>% `[`(`$`(rcorr(., type = "pearson"), "P"), 2)
            # [1] NA NA


            So, now it becomes clear that the latter doesn't work because it basically is



            `[`(mt, `$`(rcorr(mt, type = "pearson"), "P"), 2)
            # [1] NA NA


            which, when deciphered, is



            mt[rcorr(mt, type = "pearson")$P, 2]
            # [1] NA NA





            share|improve this answer














            Solution



            (mt %>% mcor(type = "pearson"))$P[2]
            # [1] 0


            Explanation



            Notice that both



            mt %>% rcorr(., type = "pearson")


            and



            mt %>% rcorr(type = "pearson")


            work as expected. The problem is that you add $ and [ to the second object, which basically are like subsequent function calls. For instance,



            s <- function(x) c(1, 1 + x)
            1 %>% s
            # [1] 1 2


            works as expected, but



            1 %>% s[1]
            # Error in .[s, 1] : incorrect number of dimensions


            doesn't return 1 since we are trying to do something like s[1](1) instead.



            Now



            1 %>% s(x = .)[1]
            # Error in .[s(x = .), 1] : incorrect number of dimensions


            just as yours



            mt %>% rcorr(., type = "pearson")$P[2]
            # [1] NA NA


            is trickier. Notice that it can be rewritten as



            mt %>% `[`(`$`(rcorr(., type = "pearson"), "P"), 2)
            # [1] NA NA


            So, now it becomes clear that the latter doesn't work because it basically is



            `[`(mt, `$`(rcorr(mt, type = "pearson"), "P"), 2)
            # [1] NA NA


            which, when deciphered, is



            mt[rcorr(mt, type = "pearson")$P, 2]
            # [1] NA NA






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 21:31

























            answered Nov 9 at 21:26









            Julius Vainora

            26.3k75877




            26.3k75877






















                up vote
                1
                down vote













                A tidy solution, at least I hope!



                library(dplyr) 
                library(broom)
                library(Hmisc)

                mtcars[, 5:6] %>%
                as.matrix()%>%
                rcorr()%>%
                tidy() %>%
                select(estimate)





                share|improve this answer


























                  up vote
                  1
                  down vote













                  A tidy solution, at least I hope!



                  library(dplyr) 
                  library(broom)
                  library(Hmisc)

                  mtcars[, 5:6] %>%
                  as.matrix()%>%
                  rcorr()%>%
                  tidy() %>%
                  select(estimate)





                  share|improve this answer
























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    A tidy solution, at least I hope!



                    library(dplyr) 
                    library(broom)
                    library(Hmisc)

                    mtcars[, 5:6] %>%
                    as.matrix()%>%
                    rcorr()%>%
                    tidy() %>%
                    select(estimate)





                    share|improve this answer














                    A tidy solution, at least I hope!



                    library(dplyr) 
                    library(broom)
                    library(Hmisc)

                    mtcars[, 5:6] %>%
                    as.matrix()%>%
                    rcorr()%>%
                    tidy() %>%
                    select(estimate)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 9 at 21:38

























                    answered Nov 9 at 21:29









                    paoloeusebi

                    30119




                    30119




















                        up vote
                        0
                        down vote













                        A simple solution using %$% from magrittr:



                        library(Hmisc)
                        library(magrittr)
                        mt <- matrix(1:10, ncol=2)

                        mt %>% rcorr(type="pearson") %$% P[2]
                        [1] 0





                        share|improve this answer


























                          up vote
                          0
                          down vote













                          A simple solution using %$% from magrittr:



                          library(Hmisc)
                          library(magrittr)
                          mt <- matrix(1:10, ncol=2)

                          mt %>% rcorr(type="pearson") %$% P[2]
                          [1] 0





                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            A simple solution using %$% from magrittr:



                            library(Hmisc)
                            library(magrittr)
                            mt <- matrix(1:10, ncol=2)

                            mt %>% rcorr(type="pearson") %$% P[2]
                            [1] 0





                            share|improve this answer














                            A simple solution using %$% from magrittr:



                            library(Hmisc)
                            library(magrittr)
                            mt <- matrix(1:10, ncol=2)

                            mt %>% rcorr(type="pearson") %$% P[2]
                            [1] 0






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 10 at 0:32

























                            answered Nov 9 at 21:47









                            Florian

                            707616




                            707616



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233231%2fpiping-into-rcorr%23new-answer', 'question_page');

                                );

                                Post as a guest














































































                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                ReactJS Fetched API data displays live - need Data displayed static

                                政党