Correlation Test loop in R










0















I am trying to create a data frame with p values and estimates that compares one gene to many different expression markers. My cor.test works when I use it on only one expression but when I try to loop it it breaks and gives me this error " 'x' and 'y' must have the same length".
I am wondering how to get this loop to work and build the data frame.



Below is what I am running through my loop and the code for the loop.



M3 <- ads$mean

Expression <- c("Exp1","Exp2","Exp3")

for (i in seq_along(Expression))

corr<-cor.test(M3, Expression[i], method = "pearson")
cor_df<-data.frame(Expression = Expression[i],pvalue = corr$p.value,
cor = corr$estimate)











share|improve this question






















  • Expression is a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation with Expression[i] doesn't make sense.

    – PoGibas
    Nov 12 '18 at 15:56











  • Expression is calling names of numeric variables from my data frame. Does that not work properly when put into the cor test? It works when I do not put it into a loop.

    – Dillon Lloyd
    Nov 12 '18 at 15:57















0















I am trying to create a data frame with p values and estimates that compares one gene to many different expression markers. My cor.test works when I use it on only one expression but when I try to loop it it breaks and gives me this error " 'x' and 'y' must have the same length".
I am wondering how to get this loop to work and build the data frame.



Below is what I am running through my loop and the code for the loop.



M3 <- ads$mean

Expression <- c("Exp1","Exp2","Exp3")

for (i in seq_along(Expression))

corr<-cor.test(M3, Expression[i], method = "pearson")
cor_df<-data.frame(Expression = Expression[i],pvalue = corr$p.value,
cor = corr$estimate)











share|improve this question






















  • Expression is a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation with Expression[i] doesn't make sense.

    – PoGibas
    Nov 12 '18 at 15:56











  • Expression is calling names of numeric variables from my data frame. Does that not work properly when put into the cor test? It works when I do not put it into a loop.

    – Dillon Lloyd
    Nov 12 '18 at 15:57













0












0








0








I am trying to create a data frame with p values and estimates that compares one gene to many different expression markers. My cor.test works when I use it on only one expression but when I try to loop it it breaks and gives me this error " 'x' and 'y' must have the same length".
I am wondering how to get this loop to work and build the data frame.



Below is what I am running through my loop and the code for the loop.



M3 <- ads$mean

Expression <- c("Exp1","Exp2","Exp3")

for (i in seq_along(Expression))

corr<-cor.test(M3, Expression[i], method = "pearson")
cor_df<-data.frame(Expression = Expression[i],pvalue = corr$p.value,
cor = corr$estimate)











share|improve this question














I am trying to create a data frame with p values and estimates that compares one gene to many different expression markers. My cor.test works when I use it on only one expression but when I try to loop it it breaks and gives me this error " 'x' and 'y' must have the same length".
I am wondering how to get this loop to work and build the data frame.



Below is what I am running through my loop and the code for the loop.



M3 <- ads$mean

Expression <- c("Exp1","Exp2","Exp3")

for (i in seq_along(Expression))

corr<-cor.test(M3, Expression[i], method = "pearson")
cor_df<-data.frame(Expression = Expression[i],pvalue = corr$p.value,
cor = corr$estimate)








r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 15:24









Dillon LloydDillon Lloyd

155




155












  • Expression is a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation with Expression[i] doesn't make sense.

    – PoGibas
    Nov 12 '18 at 15:56











  • Expression is calling names of numeric variables from my data frame. Does that not work properly when put into the cor test? It works when I do not put it into a loop.

    – Dillon Lloyd
    Nov 12 '18 at 15:57

















  • Expression is a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation with Expression[i] doesn't make sense.

    – PoGibas
    Nov 12 '18 at 15:56











  • Expression is calling names of numeric variables from my data frame. Does that not work properly when put into the cor test? It works when I do not put it into a loop.

    – Dillon Lloyd
    Nov 12 '18 at 15:57
















Expression is a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation with Expression[i] doesn't make sense.

– PoGibas
Nov 12 '18 at 15:56





Expression is a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation with Expression[i] doesn't make sense.

– PoGibas
Nov 12 '18 at 15:56













Expression is calling names of numeric variables from my data frame. Does that not work properly when put into the cor test? It works when I do not put it into a loop.

– Dillon Lloyd
Nov 12 '18 at 15:57





Expression is calling names of numeric variables from my data frame. Does that not work properly when put into the cor test? It works when I do not put it into a loop.

– Dillon Lloyd
Nov 12 '18 at 15:57












1 Answer
1






active

oldest

votes


















0














Based on your comment, if Exp1, Exp2, and Exp3 are columns in a data frame (df) then you can use something like this:



corr <- cor.test(M3, df[ ,Expression[i]], method = "pearson")





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%2f53265230%2fcorrelation-test-loop-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














    Based on your comment, if Exp1, Exp2, and Exp3 are columns in a data frame (df) then you can use something like this:



    corr <- cor.test(M3, df[ ,Expression[i]], method = "pearson")





    share|improve this answer





























      0














      Based on your comment, if Exp1, Exp2, and Exp3 are columns in a data frame (df) then you can use something like this:



      corr <- cor.test(M3, df[ ,Expression[i]], method = "pearson")





      share|improve this answer



























        0












        0








        0







        Based on your comment, if Exp1, Exp2, and Exp3 are columns in a data frame (df) then you can use something like this:



        corr <- cor.test(M3, df[ ,Expression[i]], method = "pearson")





        share|improve this answer















        Based on your comment, if Exp1, Exp2, and Exp3 are columns in a data frame (df) then you can use something like this:



        corr <- cor.test(M3, df[ ,Expression[i]], method = "pearson")






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 16:47









        989

        8,93251834




        8,93251834










        answered Nov 12 '18 at 16:11









        pooja ppooja p

        1197




        1197



























            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%2f53265230%2fcorrelation-test-loop-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

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus