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.
r
add a comment |
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.
r
1
It would be helpful to add the statementlibrary(Hmisc)
to the code in your question sincercorr
is not part of base R.
– G5W
Nov 9 at 21:42
add a comment |
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.
r
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
r
edited 2 days ago
Florian
707616
707616
asked Nov 9 at 21:02
EllieFev
537
537
1
It would be helpful to add the statementlibrary(Hmisc)
to the code in your question sincercorr
is not part of base R.
– G5W
Nov 9 at 21:42
add a comment |
1
It would be helpful to add the statementlibrary(Hmisc)
to the code in your question sincercorr
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
add a comment |
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
add a comment |
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)
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 9 at 21:31
answered Nov 9 at 21:26
Julius Vainora
26.3k75877
26.3k75877
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
A tidy solution, at least I hope!
library(dplyr)
library(broom)
library(Hmisc)
mtcars[, 5:6] %>%
as.matrix()%>%
rcorr()%>%
tidy() %>%
select(estimate)
edited Nov 9 at 21:38
answered Nov 9 at 21:29
paoloeusebi
30119
30119
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
A simple solution using %$%
from magrittr:
library(Hmisc)
library(magrittr)
mt <- matrix(1:10, ncol=2)
mt %>% rcorr(type="pearson") %$% P[2]
[1] 0
edited Nov 10 at 0:32
answered Nov 9 at 21:47
Florian
707616
707616
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%2f53233231%2fpiping-into-rcorr%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
1
It would be helpful to add the statement
library(Hmisc)
to the code in your question sincercorr
is not part of base R.– G5W
Nov 9 at 21:42