Correlation Test loop in R
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
add a comment |
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
Expressionis a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation withExpression[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
add a comment |
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
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
r
asked Nov 12 '18 at 15:24
Dillon LloydDillon Lloyd
155
155
Expressionis a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation withExpression[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
add a comment |
Expressionis a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation withExpression[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
add a comment |
1 Answer
1
active
oldest
votes
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")
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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")
add a comment |
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")
add a comment |
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")
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")
edited Nov 12 '18 at 16:47
989
8,93251834
8,93251834
answered Nov 12 '18 at 16:11
pooja ppooja p
1197
1197
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Expressionis a character vector (Expression <- c("Exp1","Exp2","Exp3")) so to calculate correlation withExpression[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