-1

Im getting an error in R program.

I installed "gapminder" and "tidyverse" packages. still problem continuous .

I typed following code in RStudio (version 1.1.463):

gapminder %>% filter (country == "India")

i got following error

Error in gapminder %>% filter(country == "India") : could not find function "%>%"
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
geeksam
  • 33
  • 8

2 Answers2

1

If we load dplyr or magrittr, it would work

library(dplyr)
library(gapminder)
gapminder %>%
    filter(country == "India")
# A tibble: 12 x 6
#   country continent  year lifeExp        pop gdpPercap
#   <fct>   <fct>     <int>   <dbl>      <int>     <dbl>
# 1 India   Asia       1952    37.4  372000000      547.
# 2 India   Asia       1957    40.2  409000000      590.
# 3 India   Asia       1962    43.6  454000000      658.
# 4 India   Asia       1967    47.2  506000000      701.
# 5 India   Asia       1972    50.7  567000000      724.
# 6 India   Asia       1977    54.2  634000000      813.
# 7 India   Asia       1982    56.6  708000000      856.
# 8 India   Asia       1987    58.6  788000000      977.
# 9 India   Asia       1992    60.2  872000000     1164.
#10 India   Asia       1997    61.8  959000000     1459.
#11 India   Asia       2002    62.9 1034172547     1747.
#12 India   Asia       2007    64.7 1110396331     2452.
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You might not have the tidyverse library loaded. Try this:

library(tidyverse)

It should fix it.

  • 2
    You don't need to load all 24 packages of the tidyverse bundle; just `dplyr` or `magrittr` will suffice – camille Sep 04 '19 at 17:05
  • Sure. But since he’s probably going to use more of the tidyverse packages, I was addressing why he had installed that particular package (in this case, collection of packages) but it didn’t work. – goncaloveiga Sep 04 '19 at 17:07
  • @goncaloveiga, not everybody has `tidyverse` installed, either due to policy (enterprise networks, classified networks, etc, all might have limitations on what can be installed) or preference (e.g., I choose to not install packages I don't use). My preference is to keep answers concise: only load libraries that are strictly required for the working code. If all an answer uses is just one function (not in this case, but ...), then there is no reason to load all packages. As much as we suggest questions should be minimal and concise, so too should be answers (imho). – r2evans Sep 04 '19 at 17:30
  • 1
    OP clearly stated in his question he had installed gapminder and tidyverse and it wasn’t working. I am answering OP’s direct question, not why he didn’t install dplyr separately or wondering about companies’ policies. – goncaloveiga Sep 04 '19 at 17:32
  • 1
    There is more of an issue here, really: the misunderstanding between "installing" and "loading" packages. My guess is that the next error will be `object 'gapminder' not found`, since I'd guess the OP also did not do `library(gapminder)`. So I think your answer is consistent with the intent of the OP. As questions go, though, this one asked about missing `%>%` which could be for many many reasons, and the resolution is simply `dplyr` or `magrittr`; perhaps adding a note in the answer-body to all of this effect would be useful, as people with similar errors would get a more targeted resolution. – r2evans Sep 04 '19 at 17:40
  • Two points and I'll stop: (1) I think your answer is in spirit with the OP, so perhaps adding a note to indicate which of the `tidyverse` packages are required would be more useful for people who come to this question on a tangent; (2) this is all a preference/style thing, I'm not down-voting or saying you're wrong. – r2evans Sep 04 '19 at 17:42
  • 1
    @r2evans agreed! Hopefully the OP has figured it out and found out he also needs to load gapminder ;) – goncaloveiga Sep 04 '19 at 17:44