Error when using purrr's map and possibly
up vote
0
down vote
favorite
I'm trying to run a looped chi-square dataframe
. I'm using map
and possibly
, both from purrr
, to allow the loop to run even if an error is thrown. Somewhere in my data.frame, I have a column that apparently has less than two values -- I can't find it. But, that's why I'm trying to run possibly
. But, I'm now getting an error that says: Can't convert a list to function. I'm not sure how to reconcile this error. I've gotten a replicable example that throws the error using the mtcars
data.frame.
library(tidyverse)
df <- mtcars %>%
mutate(z = 0)
map(df, function(x){
possibly(chisq.test(df$gear, x), otherwise = NA)
})
# Error: Can't convert a list to function
# In addition: Warning message:
# In chisq.test(df$gear, x) :
# Show Traceback
#
# Rerun with Debug
# Error: Can't convert a list to function
Any advice?
r purrr
add a comment |
up vote
0
down vote
favorite
I'm trying to run a looped chi-square dataframe
. I'm using map
and possibly
, both from purrr
, to allow the loop to run even if an error is thrown. Somewhere in my data.frame, I have a column that apparently has less than two values -- I can't find it. But, that's why I'm trying to run possibly
. But, I'm now getting an error that says: Can't convert a list to function. I'm not sure how to reconcile this error. I've gotten a replicable example that throws the error using the mtcars
data.frame.
library(tidyverse)
df <- mtcars %>%
mutate(z = 0)
map(df, function(x){
possibly(chisq.test(df$gear, x), otherwise = NA)
})
# Error: Can't convert a list to function
# In addition: Warning message:
# In chisq.test(df$gear, x) :
# Show Traceback
#
# Rerun with Debug
# Error: Can't convert a list to function
Any advice?
r purrr
Would you accept as an answer, something that returns the total number of unique values that each variable in your dataframe has?
– Harro Cyranka
Nov 10 at 23:08
This won't solve your whole problem, but I believe you needNA_real_
forotherwise
– Fons MA
Nov 10 at 23:09
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to run a looped chi-square dataframe
. I'm using map
and possibly
, both from purrr
, to allow the loop to run even if an error is thrown. Somewhere in my data.frame, I have a column that apparently has less than two values -- I can't find it. But, that's why I'm trying to run possibly
. But, I'm now getting an error that says: Can't convert a list to function. I'm not sure how to reconcile this error. I've gotten a replicable example that throws the error using the mtcars
data.frame.
library(tidyverse)
df <- mtcars %>%
mutate(z = 0)
map(df, function(x){
possibly(chisq.test(df$gear, x), otherwise = NA)
})
# Error: Can't convert a list to function
# In addition: Warning message:
# In chisq.test(df$gear, x) :
# Show Traceback
#
# Rerun with Debug
# Error: Can't convert a list to function
Any advice?
r purrr
I'm trying to run a looped chi-square dataframe
. I'm using map
and possibly
, both from purrr
, to allow the loop to run even if an error is thrown. Somewhere in my data.frame, I have a column that apparently has less than two values -- I can't find it. But, that's why I'm trying to run possibly
. But, I'm now getting an error that says: Can't convert a list to function. I'm not sure how to reconcile this error. I've gotten a replicable example that throws the error using the mtcars
data.frame.
library(tidyverse)
df <- mtcars %>%
mutate(z = 0)
map(df, function(x){
possibly(chisq.test(df$gear, x), otherwise = NA)
})
# Error: Can't convert a list to function
# In addition: Warning message:
# In chisq.test(df$gear, x) :
# Show Traceback
#
# Rerun with Debug
# Error: Can't convert a list to function
Any advice?
r purrr
r purrr
asked Nov 10 at 22:33
elliot
370112
370112
Would you accept as an answer, something that returns the total number of unique values that each variable in your dataframe has?
– Harro Cyranka
Nov 10 at 23:08
This won't solve your whole problem, but I believe you needNA_real_
forotherwise
– Fons MA
Nov 10 at 23:09
add a comment |
Would you accept as an answer, something that returns the total number of unique values that each variable in your dataframe has?
– Harro Cyranka
Nov 10 at 23:08
This won't solve your whole problem, but I believe you needNA_real_
forotherwise
– Fons MA
Nov 10 at 23:09
Would you accept as an answer, something that returns the total number of unique values that each variable in your dataframe has?
– Harro Cyranka
Nov 10 at 23:08
Would you accept as an answer, something that returns the total number of unique values that each variable in your dataframe has?
– Harro Cyranka
Nov 10 at 23:08
This won't solve your whole problem, but I believe you need
NA_real_
for otherwise
– Fons MA
Nov 10 at 23:09
This won't solve your whole problem, but I believe you need
NA_real_
for otherwise
– Fons MA
Nov 10 at 23:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The problem is in how you use possibly
. possibly
needs to wrap the function that generates the error. You are thinking that it this would be chisq.test. Not a wrong thought, because that would be my first choice as well. But inside map this is not the one that throws the error. The function you created for the .f part of the map
function throws the error. I hope my explanation is clear, but check following examples to make it a bit more clear in code.
example 1:
# Catch error of chisq.test by wrapping possibly around it
map(df, possibly(chisq.test, NA_real_), x = df$gear)
$`mpg`
Pearson's Chi-squared test
data: df$gear and .x[[i]]
X-squared = 54.667, df = 48, p-value = 0.2362
......
$z
[1] NA
Example 2 equal results:
# Catch error of created function inside map. wrap possibly around it
map(df, possibly(function(x) {
chisq.test(df$gear, x)}
, NA_real_ ))
Thanks, this solves the problem and works perfectly on my database.
– elliot
Nov 11 at 10:33
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The problem is in how you use possibly
. possibly
needs to wrap the function that generates the error. You are thinking that it this would be chisq.test. Not a wrong thought, because that would be my first choice as well. But inside map this is not the one that throws the error. The function you created for the .f part of the map
function throws the error. I hope my explanation is clear, but check following examples to make it a bit more clear in code.
example 1:
# Catch error of chisq.test by wrapping possibly around it
map(df, possibly(chisq.test, NA_real_), x = df$gear)
$`mpg`
Pearson's Chi-squared test
data: df$gear and .x[[i]]
X-squared = 54.667, df = 48, p-value = 0.2362
......
$z
[1] NA
Example 2 equal results:
# Catch error of created function inside map. wrap possibly around it
map(df, possibly(function(x) {
chisq.test(df$gear, x)}
, NA_real_ ))
Thanks, this solves the problem and works perfectly on my database.
– elliot
Nov 11 at 10:33
add a comment |
up vote
1
down vote
accepted
The problem is in how you use possibly
. possibly
needs to wrap the function that generates the error. You are thinking that it this would be chisq.test. Not a wrong thought, because that would be my first choice as well. But inside map this is not the one that throws the error. The function you created for the .f part of the map
function throws the error. I hope my explanation is clear, but check following examples to make it a bit more clear in code.
example 1:
# Catch error of chisq.test by wrapping possibly around it
map(df, possibly(chisq.test, NA_real_), x = df$gear)
$`mpg`
Pearson's Chi-squared test
data: df$gear and .x[[i]]
X-squared = 54.667, df = 48, p-value = 0.2362
......
$z
[1] NA
Example 2 equal results:
# Catch error of created function inside map. wrap possibly around it
map(df, possibly(function(x) {
chisq.test(df$gear, x)}
, NA_real_ ))
Thanks, this solves the problem and works perfectly on my database.
– elliot
Nov 11 at 10:33
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The problem is in how you use possibly
. possibly
needs to wrap the function that generates the error. You are thinking that it this would be chisq.test. Not a wrong thought, because that would be my first choice as well. But inside map this is not the one that throws the error. The function you created for the .f part of the map
function throws the error. I hope my explanation is clear, but check following examples to make it a bit more clear in code.
example 1:
# Catch error of chisq.test by wrapping possibly around it
map(df, possibly(chisq.test, NA_real_), x = df$gear)
$`mpg`
Pearson's Chi-squared test
data: df$gear and .x[[i]]
X-squared = 54.667, df = 48, p-value = 0.2362
......
$z
[1] NA
Example 2 equal results:
# Catch error of created function inside map. wrap possibly around it
map(df, possibly(function(x) {
chisq.test(df$gear, x)}
, NA_real_ ))
The problem is in how you use possibly
. possibly
needs to wrap the function that generates the error. You are thinking that it this would be chisq.test. Not a wrong thought, because that would be my first choice as well. But inside map this is not the one that throws the error. The function you created for the .f part of the map
function throws the error. I hope my explanation is clear, but check following examples to make it a bit more clear in code.
example 1:
# Catch error of chisq.test by wrapping possibly around it
map(df, possibly(chisq.test, NA_real_), x = df$gear)
$`mpg`
Pearson's Chi-squared test
data: df$gear and .x[[i]]
X-squared = 54.667, df = 48, p-value = 0.2362
......
$z
[1] NA
Example 2 equal results:
# Catch error of created function inside map. wrap possibly around it
map(df, possibly(function(x) {
chisq.test(df$gear, x)}
, NA_real_ ))
answered Nov 11 at 7:42
phiver
12k92634
12k92634
Thanks, this solves the problem and works perfectly on my database.
– elliot
Nov 11 at 10:33
add a comment |
Thanks, this solves the problem and works perfectly on my database.
– elliot
Nov 11 at 10:33
Thanks, this solves the problem and works perfectly on my database.
– elliot
Nov 11 at 10:33
Thanks, this solves the problem and works perfectly on my database.
– elliot
Nov 11 at 10:33
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244087%2ferror-when-using-purrrs-map-and-possibly%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
Would you accept as an answer, something that returns the total number of unique values that each variable in your dataframe has?
– Harro Cyranka
Nov 10 at 23:08
This won't solve your whole problem, but I believe you need
NA_real_
forotherwise
– Fons MA
Nov 10 at 23:09