尽管包含了所有必要的包,但我的目标还没有被R识别出来

时间:2018-04-19 09:17:53

标签: r

gapminder %>% + filter(country=="USA") Error in filter(., country == "USA") : object 'country' not found

我包含的图书馆: 1.dplR 2.magrittr

1 个答案:

答案 0 :(得分:1)

正如@Relasta所述,这可能是因为stats::filter()掩盖了dplyr::filter()。如果dplyr::arrange()无效,则表示您未加载dplyr:

library("gapminder")
library("magrittr")    
library("dplyr")

gapminder %>% filter(country == "United States")
# A tibble: 12 x 6
   country       continent  year lifeExp       pop gdpPercap
   <fct>         <fct>     <int>   <dbl>     <int>     <dbl>
 1 United States Americas   1952    68.4 157553000    13990.
 2 United States Americas   1957    69.5 171984000    14847.
 3 United States Americas   1962    70.2 186538000    16173.
 4 United States Americas   1967    70.8 198712000    19530.
 5 United States Americas   1972    71.3 209896000    21806.
 6 United States Americas   1977    73.4 220239000    24073.
 7 United States Americas   1982    74.6 232187835    25010.
 8 United States Americas   1987    75.0 242803533    29884.
 9 United States Americas   1992    76.1 256894189    32004.
10 United States Americas   1997    76.8 272911760    35767.
11 United States Americas   2002    77.3 287675526    39097.
12 United States Americas   2007    78.2 301139947    42952.

请务必阅读文档,因为这会向您介绍使用软件包时需要执行的操作:https://dplyr.tidyverse.org/

您正在键入(或复制粘贴)+吗?

加号(从你的问题逐字复制):

gapminder %>%
    + filter(country=="USA")
# Error in filter(country == "USA") : object 'country' not found

没有加号:

gapminder %>% filter(country == "USA")
# A tibble: 0 x 6
# ... with 6 variables: country <fct>, continent <fct>, year <int>, lifeExp <dbl>, pop <int>,
#   gdpPercap <dbl>

(你想要'美国'而不是'美国'):

gapminder %>% filter(country == "United States")
# A tibble: 12 x 6
   country       continent  year lifeExp       pop gdpPercap
   <fct>         <fct>     <int>   <dbl>     <int>     <dbl>
 1 United States Americas   1952    68.4 157553000    13990.
 2 United States Americas   1957    69.5 171984000    14847.
 3 United States Americas   1962    70.2 186538000    16173.
 4 United States Americas   1967    70.8 198712000    19530.
 5 United States Americas   1972    71.3 209896000    21806.
 6 United States Americas   1977    73.4 220239000    24073.
 7 United States Americas   1982    74.6 232187835    25010.
 8 United States Americas   1987    75.0 242803533    29884.
 9 United States Americas   1992    76.1 256894189    32004.
10 United States Americas   1997    76.8 272911760    35767.
11 United States Americas   2002    77.3 287675526    39097.
12 United States Americas   2007    78.2 301139947    42952.