[R] dplyr and function length()

Hadley Wickham h.wickham at gmail.com
Tue Aug 4 13:16:02 CEST 2015


>> length(df[,1]).
>>
>> Both commands will return n.
>>
>> However, once dplyr is loaded,
>>
>> length(df[,1]) will return a value of 1.
>>
>> length(df$m1) and also length(df[[1]]) will correctly return n.
>>
>> I know that using length() may not be the most elegant or efficient way to get the value of n. However, what puzzles (and somewhat disturbs) me is that loading of dplyr affects how length() works, without there being a warning or masking message upon loading it.
>>
>> Any clarification or comment would be welcome.
>
> Presumably, dplyr changes how [.data.frame works (by altering the default for drop=, I expect) so that df[,1] is a data frame with 1 variable and not a vector. And yes, that _is_ somewhat disturbing.

It changes the behaviour for [.tbl_df (tbl_df is a very minor
extension of data frame with custom [ and print methods).  This is
partly an experiment to see what happens when you make [ more
consistent - [.tbl_df always returns a data frame, so if you want a
vector you have to use [[.

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list