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)
}
r
add a comment |
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)
}
r
Expression
is a character vector (Expression <- c("Exp1","Exp2","Exp3")
) so to calculate correlation withExpression[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
add a comment |
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)
}
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 7 hours ago
Dillon Lloyd
155
155
Expression
is a character vector (Expression <- c("Exp1","Exp2","Exp3")
) so to calculate correlation withExpression[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
add a comment |
Expression
is a character vector (Expression <- c("Exp1","Exp2","Exp3")
) so to calculate correlation withExpression[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
add a comment |
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")
add a comment |
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")
add a comment |
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")
add a comment |
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")
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 5 hours ago
989
8,79751834
8,79751834
answered 6 hours ago
pooja p
415
415
add a comment |
add a comment |
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
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
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
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
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
Expression
is a character vector (Expression <- c("Exp1","Exp2","Exp3")
) so to calculate correlation withExpression[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