The gapminder data has class `tbl_df`, meaning it is treated as a "tibble",
but only if dplyr is loaded.  Tibble/dplyr subsetting works slightly
differently, in that it does not reduce a 1-column data frame to a vector
by default.:

```
library(gapminder)
a <- gapminder[gapminder$continent == "Africa", "gdpPercap"]
library(dplyr)
b <- gapminder[gapminder$continent == "Africa", "gdpPercap"]
str(a)
#>  num [1:624] 2449 3014 2551 3247 4183 ...
str(b)
#> Classes 'tbl_df', 'tbl' and 'data.frame':    624 obs. of  1 variable:
#>  $ gdpPercap: num  2449 3014 2551 3247 4183 ...
mean(a)
#> [1] 2193.755
mean(b)
#> Warning in mean.default(b): argument is not numeric or logical: returning
#> NA
#> [1] NA
```



On Wed, Feb 15, 2017 at 10:41 AM Christopher Hamm <[email protected]>
wrote:

> Hello all,
>
> While preparing for a workshop I was running through the code for the
> Reproducible Research with R lessons and I found an odd behavior that I
> have not encountered before in the [dplyr tutorial](
> https://swcarpentry.github.io/r-novice-gapminder/13-dplyr/).
>
> If I load load ‘gapminder' and ‘dplyr' and then run the first line of code:
>
> `mean(gapminder[gapminder$continent == "Africa", "gdpPercap"])`
>
> Returns:
>
> `[1] NA
> Warning message:
> In mean.default(gapminder[gapminder$continent == "Africa", "gdpPercap"]) :
>   argument is not numeric or logical: returning NA`
>
> If I only load ‘gapminder' the code works properly. I’ve replicated this
> on four different machines and am scratching my head as to why this error
> occurs when I load ‘dplyr’. Any thoughts?
>
> Chris Hamm
>
> _______________________________________________
> Discuss mailing list
> [email protected]
> http://lists.software-carpentry.org/listinfo/discuss
_______________________________________________
Discuss mailing list
[email protected]
http://lists.software-carpentry.org/listinfo/discuss

Reply via email to