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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 need NA_real_ for otherwise
    – 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










  • This won't solve your whole problem, but I believe you need NA_real_ for otherwise
    – 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












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_ ))





share|improve this answer





















  • Thanks, this solves the problem and works perfectly on my database.
    – elliot
    Nov 11 at 10:33











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%2f53244087%2ferror-when-using-purrrs-map-and-possibly%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








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_ ))





share|improve this answer





















  • Thanks, this solves the problem and works perfectly on my database.
    – elliot
    Nov 11 at 10:33















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_ ))





share|improve this answer





















  • Thanks, this solves the problem and works perfectly on my database.
    – elliot
    Nov 11 at 10:33













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_ ))





share|improve this answer












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_ ))






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues