P. Olsson
2007-Oct-30 14:18 UTC
[R] How to switch off accepting the shortcut of column names
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]]
Prof Brian Ripley
2007-Oct-30 15:34 UTC
[R] How to switch off accepting the shortcut of column names
If you had updated your R before posting (as the posting guide suggested) you would have found such an option under ?options. It was new in R 2.6.0. On Tue, 30 Oct 2007, 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.Well, this applies to lists not just data frame, and to partial matching not just the first letter. If you want only exact matches I suggest you use other operators, e.g. dat[["f"]] will warn in 2.6.x and fail in R-devel.> 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.Please do. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Julian Burgos
2007-Oct-30 16:59 UTC
[R] How to switch off accepting the shortcut of column names
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.