Bracket notation subsetting returns inconsistent results between data.frame and data_frame

21 views
Skip to first unread message

Thomas Hopper

unread,
May 1, 2016, 8:19:01 AM5/1/16
to manipulatr
I just spent some time trying to fix some graphing errors in my code. After pulling my hair out for a while, I eventually discovered that the errors came about because I had changed some of my code from using base R functions to using dplyr data_frame objects (class "tbl_df" "tbl" "data.frame"). data_frames don't treat bracket subsetting quite the same as a data.table.

I'm wondering if this is deliberate or an error that I should be reporting? I haven't seen it 

Example:

df <- data.frame(a = runif(5), b = rnorm(5), c = letters[1:5], stringsAsFactors = FALSE)
df[1]   # Returns a data.frame slice
df[,1]  # Returns a vector
df[[1]] # Returns a vector

library(dplyr)
df <- data_frame(a = runif(5), b = rnorm(5), c = letters[1:5], stringsAsFactors = FALSE)
df[1]   # Returns a data.frame slice, same as with data.table
df[,1]  # Returns a data.frame slice, DIFFERENT than data.table
df[[1]] # Returns a vector, same as with data.table

Is this expected? Is there a reason for this difference that I'm missing?

Thanks,

Tom

Ista Zahn

unread,
May 1, 2016, 9:33:48 AM5/1/16
to Thomas Hopper, manipulatr

Deliberate. See https://cran.r-project.org/web/packages/dplyr/vignettes/data_frames.html and ?tbl_df

Best,
Ista

--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at https://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.

Thomas Hopper

unread,
May 1, 2016, 10:36:44 AM5/1/16
to manipulatr, tomh...@gmail.com
Thank you!
Reply all
Reply to author
Forward
0 new messages