filter function in dplyr errors

前端 未结 4 731
终归单人心
终归单人心 2020-12-03 10:45

I have a data frame in R like so called UK_profiles:

row.names   id     name
1   1   8131437     Profile
2   2   8131719     WolverineCompetitio         


        
相关标签:
4条回答
  • 2020-12-03 10:56

    To understand why this happens you can recreate the error quite straightforwardly by following these steps.

    Load dplyr

    Load dplyr into a new session with only default libraries loaded, filter will work as dplyr loaded after stats

    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    
    filter(mtcars, mpg < 15)
    #>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #> 1 14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
    #> 2 10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
    #> 3 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
    #> 4 14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
    #> 5 13.3   8  350 245 3.73 3.840 15.41  0  0    3    4
    

    Unload dplyr

    This causes the error as now trying to use stats::filter. By unloading stats we see another error that there is no function called filter found at all

    detach("package:dplyr")  # Unload dplyr
    filter(mtcars, mpg < 15)  # Using stats::filter
    #> Error in filter(., mpg < 15): object 'mpg' not found
    
    detach("package:stats")  # Unload stats
    
    filter(mtcars, mpg < 15)    
    #> Error in filter(., mpg < 15): could not find function "filter"
    

    Reload stats and dplyr

    Make sure to reload dplyr after stats and we see that the dplyr version of filter works again

    library(stats)
    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> 
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    
    filter(mtcars, mpg < 15)
    #>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #> 1 14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
    #> 2 10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
    #> 3 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
    #> 4 14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
    #> 5 13.3   8  350 245 3.73 3.840 15.41  0  0    3    4
    
    0 讨论(0)
  • 2020-12-03 11:00

    It does seem like you are getting the stats::filter function and not the dplyr one. To make sure you get the right one, use the notation dplyr::filter.

    d = data.frame(x=1:10,
     name=c("foo","bar","baz","bar","bar","baz","fnord","qar","qux","quux"))
    
    filter(d, !grepl("ar|ux", name))
    Error in grepl("ar|ux", name) : object 'name' not found
    
    dplyr::filter(d, !grepl("ar|ux", name))
      x  name
    1 1   foo
    2 3   baz
    3 6   baz
    4 7 fnord
    

    You don't even need to do library(dplyr) for this to work - you do need dplyr installed though.

    This works for functions from any package.

    0 讨论(0)
  • 2020-12-03 11:03

    I think you need to both install the dplyr package with install.packages("dplyr") and then use the library command library(dplyr) to load dplyr into memory for use.
    For example the mtcars dataset is a part of dplyr, if I only install dplyr and then enter head(mtcars) it doesn't find it. Once I use the library command it is found.

    0 讨论(0)
  • 2020-12-03 11:07

    I reinstalled rlang package with restart session and it helped

    0 讨论(0)
提交回复
热议问题