[R] How to switch off accepting the shortcut of column names

Julian Burgos jmburgos at u.washington.edu
Tue Oct 30 17:59:04 CET 2007


You cannot call a column on a dataframe using the first letter (or first 
few letters) if the letters match more than one name.  Extraction 
methods for data frames allow partially matching row names, but if the 
result is undefined you get NULL in return.

  Try this.

 >first_item <- seq(1,10)
 >second_item <- seq(11,20)
 >dat <- data.frame(first_item, second_item)
 >dat$s
  [1] 11 12 13 14 15 16 17 18 19 20

 >#Now add an extra column with matching name
 >ssecond_item <- seq(21,30)
 >dat <- data.frame(dat,ssecond_item)
 >dat$s
NULL

Julian



P. Olsson wrote:
> Dear R-users,
> 
> currently I am working with the R version 2.4.1.
> I realized it has a feature,  which might be wonderful (as so many things in
> R), but in this case might be a bit dangerous as well. It seems that columns
> of a data frame can be called just by indicating the first letter of the
> name of the column.
> 
> For example:
> first_item <- seq(1,10)
> second_item <- seq(11,20)
> dat <- data.frame(first_item, second_item)
> names(dat)
> # [1] "first_item"  "second_item"
> dat$f
> # [1]  1  2  3  4  5  6  7  8  9 10
> dat$s
> #  [1] 11 12 13 14 15 16 17 18 19 20
> 
> The good thing is, that if there is more than one column starting with the
> same letter(s), more than one letter has to be given to call the column.
> However, I would appreciate if I could choose an option in my workspace,
> whether this type of shortcut is allowed or not.
> 
> Is there such an option?
> 
> Thanks for any potential hints.
> 
> Best wishes,
> 
> P. Olsson
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list