Suppose you're reading data from a file in which the column names contain underscore characters. Example: ------ start of file ----- pos_x pos_y 1.0 0.0 2.0 1.0 ------ end of file ------- Using read.table, I can read this file just fine:> data <- read.table (file="data", head=T) > datapos_x pos_y 1 1 0 2 2 1>However, I can't refer to pox_x because the _ character is an alternative to <- (assignment):> data$pos_xError: Object "x" not found I could use data[1], but I'd prefer to use the column name. Must I change all my column names to remove the underscore character or is there way to force R to recogize the whole column name? Something like data${pos_x} or data$pos\_x ??? -- Terry J. Westley, Software Systems Engineering Supervisor Veridian Engineering, Calspan Operations P.O. Box 400, Buffalo, NY 14225 twestley at buffalo.veridian.com http://www.veridian.com -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 30 Aug 1999, Terry Westley wrote:> Suppose you're reading data from a file in which the > column names contain underscore characters. Example: > > ------ start of file ----- > pos_x pos_y > 1.0 0.0 > 2.0 1.0 > ------ end of file ------- > > Using read.table, I can read this file just fine: > > > data <- read.table (file="data", head=T) > > data > pos_x pos_y > 1 1 0 > 2 2 1 > > > > However, I can't refer to pox_x because the _ character > is an alternative to <- (assignment): > > > data$pos_x > Error: Object "x" not found > > I could use data[1], but I'd prefer to use the column > name. Must I change all my column names to remove the > underscore character or is there way to force R to > recogize the whole column name? > > Something like data${pos_x} or data$pos\_x ???data$"pos_x" should work. I don't know the exact R rules, but believe that you can make quoted names work anywhere except as function argument names and in a few odd places where deparsing is done. (Library sm has to be slightly different from S to use its scripts without quotes, and they have _ in the name. -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 30 Aug 1999, Terry Westley wrote:> Suppose you're reading data from a file in which the > column names contain underscore characters. Example: > > ------ start of file ----- > pos_x pos_y > 1.0 0.0 > 2.0 1.0 > ------ end of file ------- > > Using read.table, I can read this file just fine: > > > data <- read.table (file="data", head=T) > > data > pos_x pos_y > 1 1 0 > 2 2 1 > > > > However, I can't refer to pox_x because the _ character > is an alternative to <- (assignment): > > > data$pos_x > Error: Object "x" not found > > I could use data[1], but I'd prefer to use the column > name. Must I change all my column names to remove the > underscore character or is there way to force R to > recogize the whole column name? > > Something like data${pos_x} or data$pos\_x ???Well, data$"pos_x" should work, but it would really be better to change the name to pos.x instead. I don't think work-arounds like this will be available for all R commands. For example, I don't think you can trick model formulas into accepting names with underscores in. Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks to Profs Ripley and Lumley for suggesting data$"pos_x" and to Prof Lumley for suggesting I use data$pos.x instead. After reviewing my application, I've decided that changing to data$pos.x in place of using data$"pos_x" is not too much of a hardship. Thanks for the help. -- Terry J. Westley, Software Systems Engineering Supervisor Veridian Engineering, Calspan Operations P.O. Box 400, Buffalo, NY 14225 twestley at buffalo.veridian.com http://www.veridian.com> -----Original Message----- > From: Terry Westley [mailto:twestley at buffalo.veridian.com] > Sent: Monday, August 30, 1999 1:09 PM > To: R-Help > Subject: using underscore character in column names > > > Suppose you're reading data from a file in which the > column names contain underscore characters. Example: > > ------ start of file ----- > pos_x pos_y > 1.0 0.0 > 2.0 1.0 > ------ end of file ------- > > Using read.table, I can read this file just fine: > > > data <- read.table (file="data", head=T) > > data > pos_x pos_y > 1 1 0 > 2 2 1 > > > > However, I can't refer to pox_x because the _ character > is an alternative to <- (assignment): > > > data$pos_x > Error: Object "x" not found > > I could use data[1], but I'd prefer to use the column > name. Must I change all my column names to remove the > underscore character or is there way to force R to > recogize the whole column name? > > Something like data${pos_x} or data$pos\_x ??? > > -- > Terry J. Westley, Software Systems Engineering Supervisor > Veridian Engineering, Calspan Operations > P.O. Box 400, Buffalo, NY 14225 > twestley at buffalo.veridian.com http://www.veridian.com >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._