0

I have a file wtf.r containing the following code

test <- c(1:5)
abc <- c(1:5)
df <- data.frame(test, abc)
df$te

This returns [1] 1 2 3 4 5 both interactively in RStudio and by running rscript C:/Users/xx/Documents/wtf.r, so it is not a problem with RStudio but rather with R.

Note that using df <- data.frame(test, abc) %>% as_tibble fixes the problem:

> df$te
NULL
Warning message:
Unknown or uninitialised column: `te`. 

And that this bug only exists when the column name can be autocompleted unabiguously. for example:

> test <- c(1:5)
> test2 <- c(1:5)
> df <- data.frame(test, test2) 
> df$te
NULL

Version info

> version
               _                                
platform       x86_64-w64-mingw32               
arch           x86_64                           
os             mingw32                          
crt            ucrt                             
system         x86_64, mingw32                  
status                                          
major          4                                
minor          2.1                              
year           2022                             
month          06                               
day            23                               
svn rev        82513                            
language       R                                
version.string R version 4.2.1 (2022-06-23 ucrt)
nickname       Funny-Looking Kid 

I am interested in any comments shedding some light on the root cause of this behaviour.

gaut
  • 5,771
  • 1
  • 14
  • 45
  • 1
    This is not a bug, this is a feature called _partial matching_, read `?Extract` ;) – jay.sf Sep 17 '22 at 11:45
  • 1
    Hi @gaut! You may also check some additional description in [R Language definition 3.4.1 Indexing by vectors](https://cran.r-project.org/doc/manuals/r-devel/R-lang.html#Indexing-by-vectors) (I added some pointers in the link above). Cheers – Henrik Sep 17 '22 at 13:20
  • I could have avoided loads of typing during these years. thanks – gaut Sep 17 '22 at 17:30

0 Answers0