Correlation Test loop in R











up vote
0
down vote

favorite












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
    6 hours ago










  • 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
    6 hours ago















up vote
0
down vote

favorite












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
    6 hours ago










  • 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
    6 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 7 hours ago









Dillon 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
    6 hours ago










  • 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
    6 hours ago


















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










  • 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
    6 hours ago
















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




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












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
6 hours ago




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
6 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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',
    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
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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



























      up vote
      0
      down vote













      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

























        up vote
        0
        down vote










        up vote
        0
        down vote









        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 5 hours ago









        989

        8,79751834




        8,79751834










        answered 6 hours ago









        pooja p

        415




        415






























             

            draft saved


            draft discarded



















































             


            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




















































































            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?