extract sublist of dataframes from list of dataframes based on condition












-1















I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work



cond<- sapply(ListofData, function(x) x$treatment == "ZEO") 
test <- ListofData[(cond)]


The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list



test<-ListofData[grep('^[Zeo]+',ListofData)]


Can you please help me to find a way to extract the data frames I need?










share|improve this question

























  • Maybe try ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]

    – zx8754
    Nov 19 '18 at 10:08








  • 1





    @zx8754 ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ] doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!

    – user163731
    Nov 19 '18 at 10:34
















-1















I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work



cond<- sapply(ListofData, function(x) x$treatment == "ZEO") 
test <- ListofData[(cond)]


The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list



test<-ListofData[grep('^[Zeo]+',ListofData)]


Can you please help me to find a way to extract the data frames I need?










share|improve this question

























  • Maybe try ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]

    – zx8754
    Nov 19 '18 at 10:08








  • 1





    @zx8754 ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ] doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!

    – user163731
    Nov 19 '18 at 10:34














-1












-1








-1








I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work



cond<- sapply(ListofData, function(x) x$treatment == "ZEO") 
test <- ListofData[(cond)]


The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list



test<-ListofData[grep('^[Zeo]+',ListofData)]


Can you please help me to find a way to extract the data frames I need?










share|improve this question
















I have a large list of data frames, and I want to create sub lists containing the data frames that fill a condition. Each data frame of the list has the same column names, and they have a column called treatment, which contains the word ZEO or BLEO. I'd like to be able to write a function or a one-liner that allows me to extract all the dataframes that have "ZEO". (note that one dataframe has only one treatment in it, so all the rows of the ListofData$dataframe1$treatment are equal to ZEO), The list is large (~300 dataframes) and I have other variables I'd like to be able to extract. So far I tried these methods but they didn't seem to work



cond<- sapply(ListofData, function(x) x$treatment == "ZEO") 
test <- ListofData[(cond)]


The name of the dataframes also contain the information about the treatment, that's why I tried this,but it returns an empty list



test<-ListofData[grep('^[Zeo]+',ListofData)]


Can you please help me to find a way to extract the data frames I need?







r regex list dataframe sublist






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 10:11









zx8754

29.8k76399




29.8k76399










asked Nov 19 '18 at 10:03









user163731user163731

61




61













  • Maybe try ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]

    – zx8754
    Nov 19 '18 at 10:08








  • 1





    @zx8754 ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ] doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!

    – user163731
    Nov 19 '18 at 10:34



















  • Maybe try ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]

    – zx8754
    Nov 19 '18 at 10:08








  • 1





    @zx8754 ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ] doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!

    – user163731
    Nov 19 '18 at 10:34

















Maybe try ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]

– zx8754
Nov 19 '18 at 10:08







Maybe try ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) or ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ]

– zx8754
Nov 19 '18 at 10:08






1




1





@zx8754 ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ] doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!

– user163731
Nov 19 '18 at 10:34





@zx8754 ListofData_ZEO <- ListofData[grepl('^[Zeo]+', names(ListofData)) ] doesn't work, however ListofData_ZEO <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] ) almost does the job! It gives me a list where all the dataframes I need are good and the others that i don't need are still in the list but are empty. Any idea how to make them disappear? Thank you!

– user163731
Nov 19 '18 at 10:34












1 Answer
1






active

oldest

votes


















0














Solution using regex should work, see this example:



#example data
ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
xx1 = data.frame(xx = 2, treatment = "xx"),
ZEO2= data.frame(xx = 3, treatment = "ZEO"))

#using regex
res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO


Here is another solution using column values, this returns empty dataframes, which we exclude using nrow and subset:



# using lapply, then filter
res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
res <- res[ sapply(res, nrow) > 0 ]
res
# $ZEO1
# xx treatment
# 1 1 ZEO
#
# $ZEO2
# xx treatment
# 1 3 ZEO





share|improve this answer























    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%2f53372227%2fextract-sublist-of-dataframes-from-list-of-dataframes-based-on-condition%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









    0














    Solution using regex should work, see this example:



    #example data
    ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
    xx1 = data.frame(xx = 2, treatment = "xx"),
    ZEO2= data.frame(xx = 3, treatment = "ZEO"))

    #using regex
    res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
    res
    # $ZEO1
    # xx treatment
    # 1 1 ZEO
    #
    # $ZEO2
    # xx treatment
    # 1 3 ZEO


    Here is another solution using column values, this returns empty dataframes, which we exclude using nrow and subset:



    # using lapply, then filter
    res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
    res <- res[ sapply(res, nrow) > 0 ]
    res
    # $ZEO1
    # xx treatment
    # 1 1 ZEO
    #
    # $ZEO2
    # xx treatment
    # 1 3 ZEO





    share|improve this answer




























      0














      Solution using regex should work, see this example:



      #example data
      ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
      xx1 = data.frame(xx = 2, treatment = "xx"),
      ZEO2= data.frame(xx = 3, treatment = "ZEO"))

      #using regex
      res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
      res
      # $ZEO1
      # xx treatment
      # 1 1 ZEO
      #
      # $ZEO2
      # xx treatment
      # 1 3 ZEO


      Here is another solution using column values, this returns empty dataframes, which we exclude using nrow and subset:



      # using lapply, then filter
      res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
      res <- res[ sapply(res, nrow) > 0 ]
      res
      # $ZEO1
      # xx treatment
      # 1 1 ZEO
      #
      # $ZEO2
      # xx treatment
      # 1 3 ZEO





      share|improve this answer


























        0












        0








        0







        Solution using regex should work, see this example:



        #example data
        ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
        xx1 = data.frame(xx = 2, treatment = "xx"),
        ZEO2= data.frame(xx = 3, treatment = "ZEO"))

        #using regex
        res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
        res
        # $ZEO1
        # xx treatment
        # 1 1 ZEO
        #
        # $ZEO2
        # xx treatment
        # 1 3 ZEO


        Here is another solution using column values, this returns empty dataframes, which we exclude using nrow and subset:



        # using lapply, then filter
        res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
        res <- res[ sapply(res, nrow) > 0 ]
        res
        # $ZEO1
        # xx treatment
        # 1 1 ZEO
        #
        # $ZEO2
        # xx treatment
        # 1 3 ZEO





        share|improve this answer













        Solution using regex should work, see this example:



        #example data
        ListofData <- list(ZEO1 = data.frame(xx = 1, treatment = "ZEO"),
        xx1 = data.frame(xx = 2, treatment = "xx"),
        ZEO2= data.frame(xx = 3, treatment = "ZEO"))

        #using regex
        res <- ListofData[ grepl("^[Zeo]+", names(ListofData)) ]
        res
        # $ZEO1
        # xx treatment
        # 1 1 ZEO
        #
        # $ZEO2
        # xx treatment
        # 1 3 ZEO


        Here is another solution using column values, this returns empty dataframes, which we exclude using nrow and subset:



        # using lapply, then filter
        res <- lapply(ListofData, function(x) x[ x$treatment == "ZEO", ] )
        res <- res[ sapply(res, nrow) > 0 ]
        res
        # $ZEO1
        # xx treatment
        # 1 1 ZEO
        #
        # $ZEO2
        # xx treatment
        # 1 3 ZEO






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 10:38









        zx8754zx8754

        29.8k76399




        29.8k76399
































            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%2f53372227%2fextract-sublist-of-dataframes-from-list-of-dataframes-based-on-condition%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini