Excellent explanation, thank you! CH
On February 15, 2017 at 09:55:59, François Michonneau ( [email protected]) wrote: Hi Chris, It occurs because dplyr modifies the behavior of [. When dplyr is loaded, if you select a single column from a data frame the result is a one column tibble while it's a numeric vector when it's not loaded. The latter occurs because by default (without dplyr being loaded), when selecting a single column of a data frame, the argument `drop` for the `[` function is set to TRUE leading to a vector instead of a one column data frame. dplyr is opinionated that this kind of conversions should not happen and enforces drop to be set to FALSE. > library(gapminder) > str(gapminder[gapminder$continent=="Africa", "gdpPercap"]) num [1:624] 2449 3014 2551 3247 4183 ... > library(dplyr) > str(gapminder[gapminder$continent=="Africa", "gdpPercap"]) Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 624 obs. of 1 variable: $ gdpPercap: num 2449 3014 2551 3247 4183 ... Cheers, -- François 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
