R. lapply multinomial test to list of dataframes











up vote
0
down vote

favorite












I have a data frame A, which I split into a list of 100 data frames, each having 3 rows (In my real data each data frame has 500 rows). Here I show A with 2 elements of the list (row1-row3; row4-row6):



A <- data.frame(n = c(0, 1, 2, 0, 1, 2),
prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
count = c(24878, 33605, 12100 , 25899, 34777, 13765))

# This is the list:
nest <- split(A, rep(1:2, each = 3))


I want to apply the multinomial test to each of these data frames and extract the p-value of each test. So far I have done this:



library(EMT)

fun <- function(x){
multinomial.test(x$count,
prob=x$prob,
useChisq = FALSE, MonteCarlo = TRUE,
ntrial = 100, # n of withdrawals accomplished
atOnce=100)
}

lapply(nest, fun)


However, I get:



 "Error in multinomial.test(x$counts_set, prob = x$norm_genome, useChisq = F,  : 
Observations have to be stored in a vector, e.g. 'observed <- c(5,2,1)'"


Does anyone have a smarter way of doing this?










share|improve this question
























  • I don't get an error when I run your code.
    – Florian
    Nov 10 at 5:31















up vote
0
down vote

favorite












I have a data frame A, which I split into a list of 100 data frames, each having 3 rows (In my real data each data frame has 500 rows). Here I show A with 2 elements of the list (row1-row3; row4-row6):



A <- data.frame(n = c(0, 1, 2, 0, 1, 2),
prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
count = c(24878, 33605, 12100 , 25899, 34777, 13765))

# This is the list:
nest <- split(A, rep(1:2, each = 3))


I want to apply the multinomial test to each of these data frames and extract the p-value of each test. So far I have done this:



library(EMT)

fun <- function(x){
multinomial.test(x$count,
prob=x$prob,
useChisq = FALSE, MonteCarlo = TRUE,
ntrial = 100, # n of withdrawals accomplished
atOnce=100)
}

lapply(nest, fun)


However, I get:



 "Error in multinomial.test(x$counts_set, prob = x$norm_genome, useChisq = F,  : 
Observations have to be stored in a vector, e.g. 'observed <- c(5,2,1)'"


Does anyone have a smarter way of doing this?










share|improve this question
























  • I don't get an error when I run your code.
    – Florian
    Nov 10 at 5:31













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a data frame A, which I split into a list of 100 data frames, each having 3 rows (In my real data each data frame has 500 rows). Here I show A with 2 elements of the list (row1-row3; row4-row6):



A <- data.frame(n = c(0, 1, 2, 0, 1, 2),
prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
count = c(24878, 33605, 12100 , 25899, 34777, 13765))

# This is the list:
nest <- split(A, rep(1:2, each = 3))


I want to apply the multinomial test to each of these data frames and extract the p-value of each test. So far I have done this:



library(EMT)

fun <- function(x){
multinomial.test(x$count,
prob=x$prob,
useChisq = FALSE, MonteCarlo = TRUE,
ntrial = 100, # n of withdrawals accomplished
atOnce=100)
}

lapply(nest, fun)


However, I get:



 "Error in multinomial.test(x$counts_set, prob = x$norm_genome, useChisq = F,  : 
Observations have to be stored in a vector, e.g. 'observed <- c(5,2,1)'"


Does anyone have a smarter way of doing this?










share|improve this question















I have a data frame A, which I split into a list of 100 data frames, each having 3 rows (In my real data each data frame has 500 rows). Here I show A with 2 elements of the list (row1-row3; row4-row6):



A <- data.frame(n = c(0, 1, 2, 0, 1, 2),
prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
count = c(24878, 33605, 12100 , 25899, 34777, 13765))

# This is the list:
nest <- split(A, rep(1:2, each = 3))


I want to apply the multinomial test to each of these data frames and extract the p-value of each test. So far I have done this:



library(EMT)

fun <- function(x){
multinomial.test(x$count,
prob=x$prob,
useChisq = FALSE, MonteCarlo = TRUE,
ntrial = 100, # n of withdrawals accomplished
atOnce=100)
}

lapply(nest, fun)


However, I get:



 "Error in multinomial.test(x$counts_set, prob = x$norm_genome, useChisq = F,  : 
Observations have to be stored in a vector, e.g. 'observed <- c(5,2,1)'"


Does anyone have a smarter way of doing this?







r dataframe lapply multinomial






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 10:48









Florian

1,021816




1,021816










asked Nov 10 at 0:21









Lucas

3803616




3803616












  • I don't get an error when I run your code.
    – Florian
    Nov 10 at 5:31


















  • I don't get an error when I run your code.
    – Florian
    Nov 10 at 5:31
















I don't get an error when I run your code.
– Florian
Nov 10 at 5:31




I don't get an error when I run your code.
– Florian
Nov 10 at 5:31












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










The results of split are created with names 1, 2 and so on. That's why x$count in fun cannot access it. To make it simpler, you can combine your splitted elements using the list function and then use lapply:



n <- c(0,1,2,0,1,2)
prob <- c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1)
count <- c(24878, 33605, 12100 , 25899, 34777, 13765)
A <- cbind.data.frame(n, prob, count)

nest = split(A,rep(1:2,each=3))

fun <- function(x){
multinomial.test(x$count,
prob=x$prob,
useChisq = F, MonteCarlo = TRUE,
ntrial = 100, # n of withdrawals accomplished
atOnce=100)
}

# Create a list of splitted elements
new_list <- list(nest$`1`, nest$`2`)

lapply(new_list, fun)





share|improve this answer




























    up vote
    1
    down vote













    A solution with dplyr.



    A = data.frame(n = c(0,1,2,0,1,2),
    prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
    count = c(43, 42, 9, 74, 82, 9))

    library(dplyr)
    nest <- A %>%
    mutate(pattern = rep(1:2,each=3)) %>%
    group_by(pattern) %>%
    dplyr::summarize(mn_pvals = multinomial.test(count, prob)$p.value)
    nest





    share|improve this answer





















    • Hi @paoloeusebi, your solution also works great, but Vishesh's posted a solution first. Thanks very much
      – Lucas
      Nov 10 at 22:19










    • The most important things are that you have two solutions and I have learned something new.
      – paoloeusebi
      Nov 10 at 22:21










    • Glad to hear that !
      – Lucas
      Nov 12 at 18:12











    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%2f53234912%2fr-lapply-multinomial-test-to-list-of-dataframes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The results of split are created with names 1, 2 and so on. That's why x$count in fun cannot access it. To make it simpler, you can combine your splitted elements using the list function and then use lapply:



    n <- c(0,1,2,0,1,2)
    prob <- c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1)
    count <- c(24878, 33605, 12100 , 25899, 34777, 13765)
    A <- cbind.data.frame(n, prob, count)

    nest = split(A,rep(1:2,each=3))

    fun <- function(x){
    multinomial.test(x$count,
    prob=x$prob,
    useChisq = F, MonteCarlo = TRUE,
    ntrial = 100, # n of withdrawals accomplished
    atOnce=100)
    }

    # Create a list of splitted elements
    new_list <- list(nest$`1`, nest$`2`)

    lapply(new_list, fun)





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      The results of split are created with names 1, 2 and so on. That's why x$count in fun cannot access it. To make it simpler, you can combine your splitted elements using the list function and then use lapply:



      n <- c(0,1,2,0,1,2)
      prob <- c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1)
      count <- c(24878, 33605, 12100 , 25899, 34777, 13765)
      A <- cbind.data.frame(n, prob, count)

      nest = split(A,rep(1:2,each=3))

      fun <- function(x){
      multinomial.test(x$count,
      prob=x$prob,
      useChisq = F, MonteCarlo = TRUE,
      ntrial = 100, # n of withdrawals accomplished
      atOnce=100)
      }

      # Create a list of splitted elements
      new_list <- list(nest$`1`, nest$`2`)

      lapply(new_list, fun)





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The results of split are created with names 1, 2 and so on. That's why x$count in fun cannot access it. To make it simpler, you can combine your splitted elements using the list function and then use lapply:



        n <- c(0,1,2,0,1,2)
        prob <- c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1)
        count <- c(24878, 33605, 12100 , 25899, 34777, 13765)
        A <- cbind.data.frame(n, prob, count)

        nest = split(A,rep(1:2,each=3))

        fun <- function(x){
        multinomial.test(x$count,
        prob=x$prob,
        useChisq = F, MonteCarlo = TRUE,
        ntrial = 100, # n of withdrawals accomplished
        atOnce=100)
        }

        # Create a list of splitted elements
        new_list <- list(nest$`1`, nest$`2`)

        lapply(new_list, fun)





        share|improve this answer












        The results of split are created with names 1, 2 and so on. That's why x$count in fun cannot access it. To make it simpler, you can combine your splitted elements using the list function and then use lapply:



        n <- c(0,1,2,0,1,2)
        prob <- c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1)
        count <- c(24878, 33605, 12100 , 25899, 34777, 13765)
        A <- cbind.data.frame(n, prob, count)

        nest = split(A,rep(1:2,each=3))

        fun <- function(x){
        multinomial.test(x$count,
        prob=x$prob,
        useChisq = F, MonteCarlo = TRUE,
        ntrial = 100, # n of withdrawals accomplished
        atOnce=100)
        }

        # Create a list of splitted elements
        new_list <- list(nest$`1`, nest$`2`)

        lapply(new_list, fun)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 5:10









        Vishesh Shrivastav

        1,0362621




        1,0362621
























            up vote
            1
            down vote













            A solution with dplyr.



            A = data.frame(n = c(0,1,2,0,1,2),
            prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
            count = c(43, 42, 9, 74, 82, 9))

            library(dplyr)
            nest <- A %>%
            mutate(pattern = rep(1:2,each=3)) %>%
            group_by(pattern) %>%
            dplyr::summarize(mn_pvals = multinomial.test(count, prob)$p.value)
            nest





            share|improve this answer





















            • Hi @paoloeusebi, your solution also works great, but Vishesh's posted a solution first. Thanks very much
              – Lucas
              Nov 10 at 22:19










            • The most important things are that you have two solutions and I have learned something new.
              – paoloeusebi
              Nov 10 at 22:21










            • Glad to hear that !
              – Lucas
              Nov 12 at 18:12















            up vote
            1
            down vote













            A solution with dplyr.



            A = data.frame(n = c(0,1,2,0,1,2),
            prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
            count = c(43, 42, 9, 74, 82, 9))

            library(dplyr)
            nest <- A %>%
            mutate(pattern = rep(1:2,each=3)) %>%
            group_by(pattern) %>%
            dplyr::summarize(mn_pvals = multinomial.test(count, prob)$p.value)
            nest





            share|improve this answer





















            • Hi @paoloeusebi, your solution also works great, but Vishesh's posted a solution first. Thanks very much
              – Lucas
              Nov 10 at 22:19










            • The most important things are that you have two solutions and I have learned something new.
              – paoloeusebi
              Nov 10 at 22:21










            • Glad to hear that !
              – Lucas
              Nov 12 at 18:12













            up vote
            1
            down vote










            up vote
            1
            down vote









            A solution with dplyr.



            A = data.frame(n = c(0,1,2,0,1,2),
            prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
            count = c(43, 42, 9, 74, 82, 9))

            library(dplyr)
            nest <- A %>%
            mutate(pattern = rep(1:2,each=3)) %>%
            group_by(pattern) %>%
            dplyr::summarize(mn_pvals = multinomial.test(count, prob)$p.value)
            nest





            share|improve this answer












            A solution with dplyr.



            A = data.frame(n = c(0,1,2,0,1,2),
            prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
            count = c(43, 42, 9, 74, 82, 9))

            library(dplyr)
            nest <- A %>%
            mutate(pattern = rep(1:2,each=3)) %>%
            group_by(pattern) %>%
            dplyr::summarize(mn_pvals = multinomial.test(count, prob)$p.value)
            nest






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 6:58









            paoloeusebi

            538312




            538312












            • Hi @paoloeusebi, your solution also works great, but Vishesh's posted a solution first. Thanks very much
              – Lucas
              Nov 10 at 22:19










            • The most important things are that you have two solutions and I have learned something new.
              – paoloeusebi
              Nov 10 at 22:21










            • Glad to hear that !
              – Lucas
              Nov 12 at 18:12


















            • Hi @paoloeusebi, your solution also works great, but Vishesh's posted a solution first. Thanks very much
              – Lucas
              Nov 10 at 22:19










            • The most important things are that you have two solutions and I have learned something new.
              – paoloeusebi
              Nov 10 at 22:21










            • Glad to hear that !
              – Lucas
              Nov 12 at 18:12
















            Hi @paoloeusebi, your solution also works great, but Vishesh's posted a solution first. Thanks very much
            – Lucas
            Nov 10 at 22:19




            Hi @paoloeusebi, your solution also works great, but Vishesh's posted a solution first. Thanks very much
            – Lucas
            Nov 10 at 22:19












            The most important things are that you have two solutions and I have learned something new.
            – paoloeusebi
            Nov 10 at 22:21




            The most important things are that you have two solutions and I have learned something new.
            – paoloeusebi
            Nov 10 at 22:21












            Glad to hear that !
            – Lucas
            Nov 12 at 18:12




            Glad to hear that !
            – Lucas
            Nov 12 at 18:12


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53234912%2fr-lapply-multinomial-test-to-list-of-dataframes%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

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check