Dear R People: I have a data frame, xm1, which has 12 rows and 4 columns. If I put is xm1[,-4], I get all rows, and columns 1 - 3, which is as it should be. Now, is there a way to use the names of the columns to omit them, please? Thanks so much in advance! Sincerely, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
Hi Erin, One option woild be subset(), especially the "select" parameter. HTH, Jorge On Mon, Jun 20, 2011 at 11:45 PM, Erin Hodgess <> wrote:> Dear R People: > > I have a data frame, xm1, which has 12 rows and 4 columns. > > If I put is xm1[,-4], I get all rows, and columns 1 - 3, which is as > it should be. > > Now, is there a way to use the names of the columns to omit them, please? > > Thanks so much in advance! > > Sincerely, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess@gmail.com > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi Erin, See inline. On Mon, Jun 20, 2011 at 8:45 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Dear R People: > > I have a data frame, xm1, which has 12 rows and 4 columns. > > If I put is xm1[,-4], I get all rows, and columns 1 - 3, which is as > it should be.Okay, so you know how to use the column number to omit columns.> > Now, is there a way to use the names of the columns to omit them, please?You have all the pieces (the column names, and the knowledge that you can omit columns by their index). Homework: find a way to return the column numbers given the column names (hint). Cheers, Josh> > Thanks so much in advance! > > Sincerely, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
On Mon, Jun 20, 2011 at 8:55 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Too funny! > > how about subset?Sure, that is one option. Each of the following will also work. The ones wrapped with c() can easily omit more than one at a time. mtcars[, -which(names(mtcars) == "drat")] mtcars[, names(mtcars) != "drat"] mtcars[, !names(mtcars) %in% c("drat")] mtcars[, -match(c("drat"), names(mtcars))]> > On Mon, Jun 20, 2011 at 10:52 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote: >> Hi Erin, >> >> See inline. >> >> On Mon, Jun 20, 2011 at 8:45 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote: >>> Dear R People: >>> >>> I have a data frame, xm1, which has 12 rows and 4 columns. >>> >>> If I put is xm1[,-4], I get all rows, and columns 1 - 3, which is as >>> it should be. >> >> Okay, so you know how to use the column number to omit columns. >> >>> >>> Now, is there a way to use the names of the columns to omit them, please? >> >> You have all the pieces (the column names, and the knowledge that you >> can omit columns by their index). >> >> Homework: find a way to return the column numbers given the column names (hint). >> >> Cheers, >> >> Josh >> >> >> >>> >>> Thanks so much in advance! >>> >>> Sincerely, >>> Erin >>> >>> >>> -- >>> Erin Hodgess >>> Associate Professor >>> Department of Computer and Mathematical Sciences >>> University of Houston - Downtown >>> mailto: erinm.hodgess at gmail.com >>> >>> ______________________________________________ >>> 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. >>> >> >> >> >> -- >> Joshua Wiley >> Ph.D. Student, Health Psychology >> University of California, Los Angeles >> http://www.joshuawiley.com/ >> > > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
On Jun 21, 2011, at 12:22 AM, Joshua Wiley wrote:> On Mon, Jun 20, 2011 at 8:55 PM, Erin Hodgess > <erinm.hodgess at gmail.com> wrote: >> Too funny! >> >> how about subset? > > Sure, that is one option. Each of the following will also work. The > ones wrapped with c() can easily omit more than one at a time. > > mtcars[, -which(names(mtcars) == "drat")] > mtcars[, names(mtcars) != "drat"] > mtcars[, !names(mtcars) %in% c("drat")] > mtcars[, -match(c("drat"), names(mtcars))]I like to use grep (which returns a numeric index suitable for negating) since it generalizes better: # exact match pattern: mtcars[ , -grep("^drat$", names(mtcars) ) ] #approximate, drop any name starting with "dra": mtcars[ , -grep("^dra", names(mtcars) ) ] #approximate, drop any name containing "dra": mtcars[ , -grep("drat", names(mtcars) ) ] And since the columns are actually list elements which can be addressed as numbers these are each equivalent to: # exact match pattern: mtcars[ -grep("^drat$", names(mtcars) ) ] #approximate drop any name starting with "dra": mtcars[ -grep("^dra", names(mtcars) ) ] #approximate, drop any name containing "dra": mtcars[ -grep("drat", names(mtcars) ) ] -- David> >> >> On Mon, Jun 20, 2011 at 10:52 PM, Joshua Wiley <jwiley.psych at gmail.com >> > wrote: >>> Hi Erin, >>> >>> See inline. >>> >>> On Mon, Jun 20, 2011 at 8:45 PM, Erin Hodgess <erinm.hodgess at gmail.com >>> > wrote: >>>> Dear R People: >>>> >>>> I have a data frame, xm1, which has 12 rows and 4 columns. >>>> >>>> If I put is xm1[,-4], I get all rows, and columns 1 - 3, which is >>>> as >>>> it should be. >>> >>> Okay, so you know how to use the column number to omit columns. >>> >>>> >>>> Now, is there a way to use the names of the columns to omit them, >>>> please? >>> >>> You have all the pieces (the column names, and the knowledge that >>> you >>> can omit columns by their index). >>> >>> Homework: find a way to return the column numbers given the column >>> names (hint). >>> >>> Cheers, >>> >>> Josh >>> >>> >>> >>>> >>>> Thanks so much in advance! >>>> >>>> Sincerely, >>>> Erin >>>> >>>> >>>> -- >>>> Erin Hodgess >>>> Associate Professor >>>> Department of Computer and Mathematical Sciences >>>> University of Houston - Downtown >>>> mailto: erinm.hodgess at gmail.com >>>> >>>> ______________________________________________ >>>> 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. >>>> >>> >>> >>> >>> -- >>> Joshua Wiley >>> Ph.D. Student, Health Psychology >>> University of California, Los Angeles >>> http://www.joshuawiley.com/ >>> >> >> >> >> -- >> Erin Hodgess >> Associate Professor >> Department of Computer and Mathematical Sciences >> University of Houston - Downtown >> mailto: erinm.hodgess at gmail.com >> > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > University of California, Los Angeles > http://www.joshuawiley.com/ > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Suppose names(xm1) <- c("alpha", "beta", "gamma", "delta") then xm2 <- subset(xm1, select = alpha:gamma) or xm2 <- subset(xm1, select = -delta) will do the same job as xm1[, -4] -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Erin Hodgess Sent: Tuesday, 21 June 2011 1:45 PM To: R help Subject: [R] omitting columns from a data frame Dear R People: I have a data frame, xm1, which has 12 rows and 4 columns. If I put is xm1[,-4], I get all rows, and columns 1 - 3, which is as it should be. Now, is there a way to use the names of the columns to omit them, please? Thanks so much in advance! Sincerely, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com ______________________________________________ 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.