This is probably a know problem (problem for me anyway) in R but I don't quite know what to search for in help archives. When I name a column "x11" in a data frame R thinks a column named "x1" exists. In my application I am trying to test for the existence of a column, then add it if it's not there. Here is a simple example:> temd <- data.frame(x11=c(0:10)) > is.null(temd[["x1"]])[1] FALSE> temd[["x1"]][1] 0 1 2 3 4 5 6 7 8 9 10> temd$x1[1] 0 1 2 3 4 5 6 7 8 9 10> temd[,"x1"]Error in "[.data.frame"(temd, , "x1") : undefined columns selected>This is in R 2.0.0. Anyone know how to run (safely) through a list of names like "x1"-"x100" and check for data frame columns with the names? ----------------------------------------------------------- * Opinions herein are mine only * ----------------------------------------------------------- Andrew Booker The Boeing Company P.O. Box 3707 MC 7L-22 Seattle, WA 98124-2207 425-865-3573 FAX# 425-865-2966 andrew.j.booker at boeing.com
On Tue, 5 Oct 2004, Booker, Andrew J wrote:> This is probably a know problem (problem for me anyway) in R but I > don't quite know what to search for in help archives. When I name a > column "x11" in a data frame R thinks a column named "x1" exists. In > my application I am trying to test for the existence of a column, then > add it if it's not there. Here is a simple example: > >> temd <- data.frame(x11=c(0:10)) >> is.null(temd[["x1"]]) > [1] FALSEYes. R allows partial matching in this and many other contexts. You could use "x1" %in% names(temd) to test for the name without partial matching (probably more efficiently if the data frame is large) -thomas
How about: > DF <- data.frame(x11=1) > c("x1", "x11") %in% names(DF) [1] FALSE TRUE hope this helps. spencer graves Booker, Andrew J wrote:>This is probably a know problem (problem for me anyway) in R but I >don't quite know what to search for in help archives. When I name a >column "x11" in a data frame R thinks a column named "x1" exists. In >my application I am trying to test for the existence of a column, then >add it if it's not there. Here is a simple example: > > > >>temd <- data.frame(x11=c(0:10)) >>is.null(temd[["x1"]]) >> >> >[1] FALSE > > >>temd[["x1"]] >> >> > [1] 0 1 2 3 4 5 6 7 8 9 10 > > >>temd$x1 >> >> > [1] 0 1 2 3 4 5 6 7 8 9 10 > > >>temd[,"x1"] >> >> >Error in "[.data.frame"(temd, , "x1") : undefined columns selected > > > >This is in R 2.0.0. > >Anyone know how to run (safely) through a list of names like "x1"-"x100" and check for data frame columns with the names? > > > >----------------------------------------------------------- >* Opinions herein are mine only * >----------------------------------------------------------- > > Andrew Booker > The Boeing Company > P.O. Box 3707 MC 7L-22 > Seattle, WA 98124-2207 > 425-865-3573 > FAX# 425-865-2966 > andrew.j.booker at boeing.com > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567
Booker, Andrew J wrote:> This is probably a know problem (problem for me anyway) in R but I > don't quite know what to search for in help archives. When I name a > column "x11" in a data frame R thinks a column named "x1" exists.Not exactly. R uses partial matching here. Iff "x11" and "x1" exists, "x11" will be selected. Pretty much like matching for arguments in functions. Probably you are looking for names(temd)? Uwe Ligges > In> my application I am trying to test for the existence of a column, then > add it if it's not there. Here is a simple example: > > >>temd <- data.frame(x11=c(0:10)) >>is.null(temd[["x1"]]) > > [1] FALSE > >>temd[["x1"]] > > [1] 0 1 2 3 4 5 6 7 8 9 10 > >>temd$x1 > > [1] 0 1 2 3 4 5 6 7 8 9 10 > >>temd[,"x1"] > > Error in "[.data.frame"(temd, , "x1") : undefined columns selected > > > This is in R 2.0.0. > > Anyone know how to run (safely) through a list of names like "x1"-"x100" and check for data frame columns with the names? > > > > ----------------------------------------------------------- > * Opinions herein are mine only * > ----------------------------------------------------------- > > Andrew Booker > The Boeing Company > P.O. Box 3707 MC 7L-22 > Seattle, WA 98124-2207 > 425-865-3573 > FAX# 425-865-2966 > andrew.j.booker at boeing.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html