Lapply in a nested list in R












0















I'm trying to apply a function to a nested list. I have the following lists:



lista_a <- list("PEC", "45", "1991")
lista_b <- list("PL", "4580", "1990")
lista_c <- list("PL", "200", "1980")


Which are nested in the following list:



lista_final <- list(lista_a, lista_b, lista_c)


I want to apply the following function (which uses the function cham_votes from congressbr package):



funcao <- function(x){ tryCatch(do.call(cham_votes, x), error=function(e){NA})}


To each element of lista_final. I am trying to use do.call because cham_votes has three inputs (type, number and year) and I want to use them all at the same time, therefore I needed a list.



Do you have any idea how can I apply this function to all elements of lista_final at once? The final result should be a list of dataframes.



Thanks for your help.










share|improve this question























  • I get: "Package ‘congressbr’ was removed from the CRAN repository." when attempting to find that package. If you have a package that is not (currently) in CRAN then you need to say where it is and what dependencies it might have.

    – 42-
    Nov 14 '18 at 2:57













  • Sorry, next time I'll pay attention on this. Thanks for your comment!

    – rdgsmateus
    Nov 15 '18 at 20:40
















0















I'm trying to apply a function to a nested list. I have the following lists:



lista_a <- list("PEC", "45", "1991")
lista_b <- list("PL", "4580", "1990")
lista_c <- list("PL", "200", "1980")


Which are nested in the following list:



lista_final <- list(lista_a, lista_b, lista_c)


I want to apply the following function (which uses the function cham_votes from congressbr package):



funcao <- function(x){ tryCatch(do.call(cham_votes, x), error=function(e){NA})}


To each element of lista_final. I am trying to use do.call because cham_votes has three inputs (type, number and year) and I want to use them all at the same time, therefore I needed a list.



Do you have any idea how can I apply this function to all elements of lista_final at once? The final result should be a list of dataframes.



Thanks for your help.










share|improve this question























  • I get: "Package ‘congressbr’ was removed from the CRAN repository." when attempting to find that package. If you have a package that is not (currently) in CRAN then you need to say where it is and what dependencies it might have.

    – 42-
    Nov 14 '18 at 2:57













  • Sorry, next time I'll pay attention on this. Thanks for your comment!

    – rdgsmateus
    Nov 15 '18 at 20:40














0












0








0








I'm trying to apply a function to a nested list. I have the following lists:



lista_a <- list("PEC", "45", "1991")
lista_b <- list("PL", "4580", "1990")
lista_c <- list("PL", "200", "1980")


Which are nested in the following list:



lista_final <- list(lista_a, lista_b, lista_c)


I want to apply the following function (which uses the function cham_votes from congressbr package):



funcao <- function(x){ tryCatch(do.call(cham_votes, x), error=function(e){NA})}


To each element of lista_final. I am trying to use do.call because cham_votes has three inputs (type, number and year) and I want to use them all at the same time, therefore I needed a list.



Do you have any idea how can I apply this function to all elements of lista_final at once? The final result should be a list of dataframes.



Thanks for your help.










share|improve this question














I'm trying to apply a function to a nested list. I have the following lists:



lista_a <- list("PEC", "45", "1991")
lista_b <- list("PL", "4580", "1990")
lista_c <- list("PL", "200", "1980")


Which are nested in the following list:



lista_final <- list(lista_a, lista_b, lista_c)


I want to apply the following function (which uses the function cham_votes from congressbr package):



funcao <- function(x){ tryCatch(do.call(cham_votes, x), error=function(e){NA})}


To each element of lista_final. I am trying to use do.call because cham_votes has three inputs (type, number and year) and I want to use them all at the same time, therefore I needed a list.



Do you have any idea how can I apply this function to all elements of lista_final at once? The final result should be a list of dataframes.



Thanks for your help.







r try-catch lapply nested-lists do.call






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 2:36









rdgsmateusrdgsmateus

13




13













  • I get: "Package ‘congressbr’ was removed from the CRAN repository." when attempting to find that package. If you have a package that is not (currently) in CRAN then you need to say where it is and what dependencies it might have.

    – 42-
    Nov 14 '18 at 2:57













  • Sorry, next time I'll pay attention on this. Thanks for your comment!

    – rdgsmateus
    Nov 15 '18 at 20:40



















  • I get: "Package ‘congressbr’ was removed from the CRAN repository." when attempting to find that package. If you have a package that is not (currently) in CRAN then you need to say where it is and what dependencies it might have.

    – 42-
    Nov 14 '18 at 2:57













  • Sorry, next time I'll pay attention on this. Thanks for your comment!

    – rdgsmateus
    Nov 15 '18 at 20:40

















I get: "Package ‘congressbr’ was removed from the CRAN repository." when attempting to find that package. If you have a package that is not (currently) in CRAN then you need to say where it is and what dependencies it might have.

– 42-
Nov 14 '18 at 2:57







I get: "Package ‘congressbr’ was removed from the CRAN repository." when attempting to find that package. If you have a package that is not (currently) in CRAN then you need to say where it is and what dependencies it might have.

– 42-
Nov 14 '18 at 2:57















Sorry, next time I'll pay attention on this. Thanks for your comment!

– rdgsmateus
Nov 15 '18 at 20:40





Sorry, next time I'll pay attention on this. Thanks for your comment!

– rdgsmateus
Nov 15 '18 at 20:40












1 Answer
1






active

oldest

votes


















-1














The function cham_votes() expects the last two arguments to be integers. So, you have to convert them first as follows:



listafinal <- lapply(listafinal, function(x) c(x[1], lapply(x[2:3], as.integer)))


You need then to simply use lapply.



lapply(lista_final, funcao)


This will return a list of elements each of which is of the same type as the return value of your function funcao() which is either NA or a tibble of classes tbl, tbl_df, and dataframe






share|improve this answer


























  • The proposed code would have expected an argument to be a list. So a single lapply should have been enough.

    – 42-
    Nov 14 '18 at 2:55











  • Yeah, I see. edited thanks

    – Abdallah Atef
    Nov 14 '18 at 3:03











  • Thanks for your help!

    – rdgsmateus
    Nov 14 '18 at 17:00











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292404%2flapply-in-a-nested-list-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









-1














The function cham_votes() expects the last two arguments to be integers. So, you have to convert them first as follows:



listafinal <- lapply(listafinal, function(x) c(x[1], lapply(x[2:3], as.integer)))


You need then to simply use lapply.



lapply(lista_final, funcao)


This will return a list of elements each of which is of the same type as the return value of your function funcao() which is either NA or a tibble of classes tbl, tbl_df, and dataframe






share|improve this answer


























  • The proposed code would have expected an argument to be a list. So a single lapply should have been enough.

    – 42-
    Nov 14 '18 at 2:55











  • Yeah, I see. edited thanks

    – Abdallah Atef
    Nov 14 '18 at 3:03











  • Thanks for your help!

    – rdgsmateus
    Nov 14 '18 at 17:00
















-1














The function cham_votes() expects the last two arguments to be integers. So, you have to convert them first as follows:



listafinal <- lapply(listafinal, function(x) c(x[1], lapply(x[2:3], as.integer)))


You need then to simply use lapply.



lapply(lista_final, funcao)


This will return a list of elements each of which is of the same type as the return value of your function funcao() which is either NA or a tibble of classes tbl, tbl_df, and dataframe






share|improve this answer


























  • The proposed code would have expected an argument to be a list. So a single lapply should have been enough.

    – 42-
    Nov 14 '18 at 2:55











  • Yeah, I see. edited thanks

    – Abdallah Atef
    Nov 14 '18 at 3:03











  • Thanks for your help!

    – rdgsmateus
    Nov 14 '18 at 17:00














-1












-1








-1







The function cham_votes() expects the last two arguments to be integers. So, you have to convert them first as follows:



listafinal <- lapply(listafinal, function(x) c(x[1], lapply(x[2:3], as.integer)))


You need then to simply use lapply.



lapply(lista_final, funcao)


This will return a list of elements each of which is of the same type as the return value of your function funcao() which is either NA or a tibble of classes tbl, tbl_df, and dataframe






share|improve this answer















The function cham_votes() expects the last two arguments to be integers. So, you have to convert them first as follows:



listafinal <- lapply(listafinal, function(x) c(x[1], lapply(x[2:3], as.integer)))


You need then to simply use lapply.



lapply(lista_final, funcao)


This will return a list of elements each of which is of the same type as the return value of your function funcao() which is either NA or a tibble of classes tbl, tbl_df, and dataframe







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 4:20

























answered Nov 14 '18 at 2:50









Abdallah AtefAbdallah Atef

4731110




4731110













  • The proposed code would have expected an argument to be a list. So a single lapply should have been enough.

    – 42-
    Nov 14 '18 at 2:55











  • Yeah, I see. edited thanks

    – Abdallah Atef
    Nov 14 '18 at 3:03











  • Thanks for your help!

    – rdgsmateus
    Nov 14 '18 at 17:00



















  • The proposed code would have expected an argument to be a list. So a single lapply should have been enough.

    – 42-
    Nov 14 '18 at 2:55











  • Yeah, I see. edited thanks

    – Abdallah Atef
    Nov 14 '18 at 3:03











  • Thanks for your help!

    – rdgsmateus
    Nov 14 '18 at 17:00

















The proposed code would have expected an argument to be a list. So a single lapply should have been enough.

– 42-
Nov 14 '18 at 2:55





The proposed code would have expected an argument to be a list. So a single lapply should have been enough.

– 42-
Nov 14 '18 at 2:55













Yeah, I see. edited thanks

– Abdallah Atef
Nov 14 '18 at 3:03





Yeah, I see. edited thanks

– Abdallah Atef
Nov 14 '18 at 3:03













Thanks for your help!

– rdgsmateus
Nov 14 '18 at 17:00





Thanks for your help!

– rdgsmateus
Nov 14 '18 at 17:00


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292404%2flapply-in-a-nested-list-in-r%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

Lugert, Oklahoma