Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as.data.frame.tbl_df() ignores row.names argument #1202

Open
shivam7898 opened this issue Jul 6, 2022 · 5 comments · Fixed by #1504 · May be fixed by #1507
Open

as.data.frame.tbl_df() ignores row.names argument #1202

shivam7898 opened this issue Jul 6, 2022 · 5 comments · Fixed by #1504 · May be fixed by #1507

Comments

@shivam7898
Copy link

Help says that for as.data.frame(), x can be any R object and row.names can be a character vector giving the row names for the data frame.

Although tibble has a valid opinion on rownames, the output is a data.frame. So, I believe that the as.data.frame.tbl_df() method should not ignore row.names argument and set the attribute like as.data.frame.data.frame() does.

Obviously, I am not the expert so I am willing to be corrected. Thank You :)

library(tibble)

# as.data.frame.tbl_df() ignores 'row.names'
as.data.frame(tibble(x = 1:2), row.names = letters[1:2]) 
#  x
#1 1
#2 2

# as.data.frame.data.frame() does not ignore 'row.names'
as.data.frame(data.frame(x = 1:2), row.names = letters[1:2])
#   x
# a 1
# b 2

sessionInfo()

sessionInfo()
# R version 4.2.1 (2022-06-23 ucrt)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19044)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=English_India.utf8  LC_CTYPE=English_India.utf8   
# [3] LC_MONETARY=English_India.utf8 LC_NUMERIC=C                  
# [5] LC_TIME=English_India.utf8    
# 
# attached base packages:
# [1] stats     graphics  grDevices datasets  utils     methods   base     
# 
# other attached packages:
# [1] tibble_3.1.7
# 
# loaded via a namespace (and not attached):
#  [1] compiler_4.2.1  magrittr_2.0.3  ellipsis_0.3.2  cli_3.3.0      
#  [5] pillar_1.7.0    glue_1.6.2      crayon_1.5.1    utf8_1.2.2     
#  [9] fansi_1.0.3     vctrs_0.4.1     lifecycle_1.0.1 pkgconfig_2.0.3
# [13] rlang_1.0.3 
@krlmlr
Copy link
Member

krlmlr commented Dec 4, 2022

Thanks. This has been like this from day one, but looks like something we could consider. On the other hand, there's column_to_rownames() that achieves a variant of this today.

@hadley @DavisVaughan: What are your thoughts on supporting as.data.frame.tbl_df(x, row.names = ...) for assigning row names to the resulting data frame?

@hadley
Copy link
Member

hadley commented Dec 4, 2022

If it's simple to implement and doesn't affect much downstream code, it seems like it's worth adhering to the as.data.frame API contract (such as it is).

@krlmlr
Copy link
Member

krlmlr commented Feb 22, 2023

Reverted because it's causing downstream failures. Will address in a separate PR after CRAN release.

@hadley
Copy link
Member

hadley commented Feb 22, 2023

If it's going to cause revdep failures, that clearly pushes it in to the "not worth it" category, IMO.

@BartJanvanRossum
Copy link

BartJanvanRossum commented Feb 23, 2023

I just ran into this as well. Even though I agree that it might not be worth changing, please at least consider documenting this. Now the function arguments suggest functionality that is not implemented which is confusing for me as a user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants