extract sublist of dataframes from list of dataframes based on condition











up vote
-1
down vote

favorite












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









New contributor




user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















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








  • 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
    yesterday















up vote
-1
down vote

favorite












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









New contributor




user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















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








  • 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
    yesterday













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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









New contributor




user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









zx8754

28.5k76394




28.5k76394






New contributor




user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









user163731

61




61




New contributor




user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user163731 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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








  • 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
    yesterday


















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








  • 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
    yesterday
















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






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






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
yesterday




@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
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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


    }
    });






    user163731 is a new contributor. Be nice, and check out our Code of Conduct.










     

    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








    up vote
    0
    down vote



    accepted










    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

























      up vote
      0
      down vote



      accepted










      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























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        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 yesterday









        zx8754

        28.5k76394




        28.5k76394






















            user163731 is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            user163731 is a new contributor. Be nice, and check out our Code of Conduct.













            user163731 is a new contributor. Be nice, and check out our Code of Conduct.












            user163731 is a new contributor. Be nice, and check out our Code of Conduct.















             


            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







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]