R: error when trying a loop with dplyr filter function












0















Trying to simplify and make my code reproducible, I have the following loop code which aims to subset a data frame and store every subsetting in a new variable:



types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

for (i in seq_along(types)) {
paste("type", types[i], sep = "") <- filter(NEI$Emissions, NEI$type == types[i])
}


I expected my loop to store every subsetting (4 subsettings) in a new variable called "type" and the corresponding string. Instead of it, I get the following error:




"Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




I've already tried to modify the class of type coercing it to be a string through as.character(types) but got no success.



Edit: the output of head(NEI) is the following:



   fips      SCC  Pollutant Emissions  type year
4 09001 10100401 PM25-PRI 15.714 POINT 1999
8 09001 10100404 PM25-PRI 234.178 POINT 1999
12 09001 10100501 PM25-PRI 0.128 POINT 1999
16 09001 10200401 PM25-PRI 2.036 POINT 1999
20 09001 10200504 PM25-PRI 0.388 POINT 1999
24 09001 10200602 PM25-PRI 1.490 POINT 1999









share|improve this question




















  • 2





    Mostly you should get what you want by doing split(NEI$Emissions, NEI$type)

    – Ronak Shah
    Jan 2 at 4:50











  • can you add the output from dput(head(data)) so we can help more easily?

    – RAB
    Jan 2 at 4:54











  • just done, chek my edit. @RAB

    – Mauro
    Jan 2 at 4:59











  • I guess what you intend to achieve requires assign

    – MarBlo
    Jan 2 at 5:05






  • 2





    @Mauro then do split(NEI, NEI$type)

    – Ronak Shah
    Jan 2 at 5:07
















0















Trying to simplify and make my code reproducible, I have the following loop code which aims to subset a data frame and store every subsetting in a new variable:



types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

for (i in seq_along(types)) {
paste("type", types[i], sep = "") <- filter(NEI$Emissions, NEI$type == types[i])
}


I expected my loop to store every subsetting (4 subsettings) in a new variable called "type" and the corresponding string. Instead of it, I get the following error:




"Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




I've already tried to modify the class of type coercing it to be a string through as.character(types) but got no success.



Edit: the output of head(NEI) is the following:



   fips      SCC  Pollutant Emissions  type year
4 09001 10100401 PM25-PRI 15.714 POINT 1999
8 09001 10100404 PM25-PRI 234.178 POINT 1999
12 09001 10100501 PM25-PRI 0.128 POINT 1999
16 09001 10200401 PM25-PRI 2.036 POINT 1999
20 09001 10200504 PM25-PRI 0.388 POINT 1999
24 09001 10200602 PM25-PRI 1.490 POINT 1999









share|improve this question




















  • 2





    Mostly you should get what you want by doing split(NEI$Emissions, NEI$type)

    – Ronak Shah
    Jan 2 at 4:50











  • can you add the output from dput(head(data)) so we can help more easily?

    – RAB
    Jan 2 at 4:54











  • just done, chek my edit. @RAB

    – Mauro
    Jan 2 at 4:59











  • I guess what you intend to achieve requires assign

    – MarBlo
    Jan 2 at 5:05






  • 2





    @Mauro then do split(NEI, NEI$type)

    – Ronak Shah
    Jan 2 at 5:07














0












0








0








Trying to simplify and make my code reproducible, I have the following loop code which aims to subset a data frame and store every subsetting in a new variable:



types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

for (i in seq_along(types)) {
paste("type", types[i], sep = "") <- filter(NEI$Emissions, NEI$type == types[i])
}


I expected my loop to store every subsetting (4 subsettings) in a new variable called "type" and the corresponding string. Instead of it, I get the following error:




"Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




I've already tried to modify the class of type coercing it to be a string through as.character(types) but got no success.



Edit: the output of head(NEI) is the following:



   fips      SCC  Pollutant Emissions  type year
4 09001 10100401 PM25-PRI 15.714 POINT 1999
8 09001 10100404 PM25-PRI 234.178 POINT 1999
12 09001 10100501 PM25-PRI 0.128 POINT 1999
16 09001 10200401 PM25-PRI 2.036 POINT 1999
20 09001 10200504 PM25-PRI 0.388 POINT 1999
24 09001 10200602 PM25-PRI 1.490 POINT 1999









share|improve this question
















Trying to simplify and make my code reproducible, I have the following loop code which aims to subset a data frame and store every subsetting in a new variable:



types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

for (i in seq_along(types)) {
paste("type", types[i], sep = "") <- filter(NEI$Emissions, NEI$type == types[i])
}


I expected my loop to store every subsetting (4 subsettings) in a new variable called "type" and the corresponding string. Instead of it, I get the following error:




"Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




I've already tried to modify the class of type coercing it to be a string through as.character(types) but got no success.



Edit: the output of head(NEI) is the following:



   fips      SCC  Pollutant Emissions  type year
4 09001 10100401 PM25-PRI 15.714 POINT 1999
8 09001 10100404 PM25-PRI 234.178 POINT 1999
12 09001 10100501 PM25-PRI 0.128 POINT 1999
16 09001 10200401 PM25-PRI 2.036 POINT 1999
20 09001 10200504 PM25-PRI 0.388 POINT 1999
24 09001 10200602 PM25-PRI 1.490 POINT 1999






r for-loop subset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 5:04









rar

6331515




6331515










asked Jan 2 at 4:47









MauroMauro

84112




84112








  • 2





    Mostly you should get what you want by doing split(NEI$Emissions, NEI$type)

    – Ronak Shah
    Jan 2 at 4:50











  • can you add the output from dput(head(data)) so we can help more easily?

    – RAB
    Jan 2 at 4:54











  • just done, chek my edit. @RAB

    – Mauro
    Jan 2 at 4:59











  • I guess what you intend to achieve requires assign

    – MarBlo
    Jan 2 at 5:05






  • 2





    @Mauro then do split(NEI, NEI$type)

    – Ronak Shah
    Jan 2 at 5:07














  • 2





    Mostly you should get what you want by doing split(NEI$Emissions, NEI$type)

    – Ronak Shah
    Jan 2 at 4:50











  • can you add the output from dput(head(data)) so we can help more easily?

    – RAB
    Jan 2 at 4:54











  • just done, chek my edit. @RAB

    – Mauro
    Jan 2 at 4:59











  • I guess what you intend to achieve requires assign

    – MarBlo
    Jan 2 at 5:05






  • 2





    @Mauro then do split(NEI, NEI$type)

    – Ronak Shah
    Jan 2 at 5:07








2




2





Mostly you should get what you want by doing split(NEI$Emissions, NEI$type)

– Ronak Shah
Jan 2 at 4:50





Mostly you should get what you want by doing split(NEI$Emissions, NEI$type)

– Ronak Shah
Jan 2 at 4:50













can you add the output from dput(head(data)) so we can help more easily?

– RAB
Jan 2 at 4:54





can you add the output from dput(head(data)) so we can help more easily?

– RAB
Jan 2 at 4:54













just done, chek my edit. @RAB

– Mauro
Jan 2 at 4:59





just done, chek my edit. @RAB

– Mauro
Jan 2 at 4:59













I guess what you intend to achieve requires assign

– MarBlo
Jan 2 at 5:05





I guess what you intend to achieve requires assign

– MarBlo
Jan 2 at 5:05




2




2





@Mauro then do split(NEI, NEI$type)

– Ronak Shah
Jan 2 at 5:07





@Mauro then do split(NEI, NEI$type)

– Ronak Shah
Jan 2 at 5:07












1 Answer
1






active

oldest

votes


















0














What you want to do is



types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

for (i in types) {
assign(paste0("type", i), filter(NEI, NEI$type == i))
}


This will give you 4 dataframes based on types



Trying this on mtcars dataset



cyl <- unique(mtcars$cyl)

for (i in cyl) {
assign(paste0("type", i), filter(mtcars, mtcars$cyl == i))
}

type6
# mpg cyl disp hp drat wt qsec vs am gear carb
#1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#4 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#5 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
#6 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
#7 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6


and similarly other dataframes.



The reason why your solution does not work is because you are trying to assign values to characters which can be reproduced by doing



paste0("type", types[1]) <- filter(mtcars$mpg, mtcars$cyl == 4)



Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




However, using assign is bad, as mentioned in comments you can use split which would give you list of dataframes.



new_data <- split(NEI, NEI$type)


and then access them by doing new_data[[1]], new_data[[2]] and so on.






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%2f54001322%2fr-error-when-trying-a-loop-with-dplyr-filter-function%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














    What you want to do is



    types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

    for (i in types) {
    assign(paste0("type", i), filter(NEI, NEI$type == i))
    }


    This will give you 4 dataframes based on types



    Trying this on mtcars dataset



    cyl <- unique(mtcars$cyl)

    for (i in cyl) {
    assign(paste0("type", i), filter(mtcars, mtcars$cyl == i))
    }

    type6
    # mpg cyl disp hp drat wt qsec vs am gear carb
    #1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
    #2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
    #3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
    #4 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
    #5 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
    #6 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
    #7 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6


    and similarly other dataframes.



    The reason why your solution does not work is because you are trying to assign values to characters which can be reproduced by doing



    paste0("type", types[1]) <- filter(mtcars$mpg, mtcars$cyl == 4)



    Error in UseMethod("filter_") :
    no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




    However, using assign is bad, as mentioned in comments you can use split which would give you list of dataframes.



    new_data <- split(NEI, NEI$type)


    and then access them by doing new_data[[1]], new_data[[2]] and so on.






    share|improve this answer




























      0














      What you want to do is



      types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

      for (i in types) {
      assign(paste0("type", i), filter(NEI, NEI$type == i))
      }


      This will give you 4 dataframes based on types



      Trying this on mtcars dataset



      cyl <- unique(mtcars$cyl)

      for (i in cyl) {
      assign(paste0("type", i), filter(mtcars, mtcars$cyl == i))
      }

      type6
      # mpg cyl disp hp drat wt qsec vs am gear carb
      #1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
      #2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
      #3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
      #4 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
      #5 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
      #6 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
      #7 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6


      and similarly other dataframes.



      The reason why your solution does not work is because you are trying to assign values to characters which can be reproduced by doing



      paste0("type", types[1]) <- filter(mtcars$mpg, mtcars$cyl == 4)



      Error in UseMethod("filter_") :
      no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




      However, using assign is bad, as mentioned in comments you can use split which would give you list of dataframes.



      new_data <- split(NEI, NEI$type)


      and then access them by doing new_data[[1]], new_data[[2]] and so on.






      share|improve this answer


























        0












        0








        0







        What you want to do is



        types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

        for (i in types) {
        assign(paste0("type", i), filter(NEI, NEI$type == i))
        }


        This will give you 4 dataframes based on types



        Trying this on mtcars dataset



        cyl <- unique(mtcars$cyl)

        for (i in cyl) {
        assign(paste0("type", i), filter(mtcars, mtcars$cyl == i))
        }

        type6
        # mpg cyl disp hp drat wt qsec vs am gear carb
        #1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
        #2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
        #3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
        #4 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
        #5 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
        #6 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
        #7 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6


        and similarly other dataframes.



        The reason why your solution does not work is because you are trying to assign values to characters which can be reproduced by doing



        paste0("type", types[1]) <- filter(mtcars$mpg, mtcars$cyl == 4)



        Error in UseMethod("filter_") :
        no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




        However, using assign is bad, as mentioned in comments you can use split which would give you list of dataframes.



        new_data <- split(NEI, NEI$type)


        and then access them by doing new_data[[1]], new_data[[2]] and so on.






        share|improve this answer













        What you want to do is



        types <- c("POINT", "NONPOINT", "ON-ROAD", "NON-ROAD")

        for (i in types) {
        assign(paste0("type", i), filter(NEI, NEI$type == i))
        }


        This will give you 4 dataframes based on types



        Trying this on mtcars dataset



        cyl <- unique(mtcars$cyl)

        for (i in cyl) {
        assign(paste0("type", i), filter(mtcars, mtcars$cyl == i))
        }

        type6
        # mpg cyl disp hp drat wt qsec vs am gear carb
        #1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
        #2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
        #3 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
        #4 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
        #5 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
        #6 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
        #7 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6


        and similarly other dataframes.



        The reason why your solution does not work is because you are trying to assign values to characters which can be reproduced by doing



        paste0("type", types[1]) <- filter(mtcars$mpg, mtcars$cyl == 4)



        Error in UseMethod("filter_") :
        no applicable method for 'filter_' applied to an object of class "c('double', 'numeric')"




        However, using assign is bad, as mentioned in comments you can use split which would give you list of dataframes.



        new_data <- split(NEI, NEI$type)


        and then access them by doing new_data[[1]], new_data[[2]] and so on.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 5:41









        Ronak ShahRonak Shah

        39.2k104162




        39.2k104162
































            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%2f54001322%2fr-error-when-trying-a-loop-with-dplyr-filter-function%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas