x[?V2?] would retain columns of x headed by V2. What I need is the opposite??I need a data grime with those columns excluded. Steven from iPhone> On Feb 13, 2023, at 9:33 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote: > > ? >> On Sun, 12 Feb 2023 14:57:36 -0800 >> Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: >> >> x["V2"] >> >> is more efficient than using drop=FALSE, and perfectly normal syntax >> (data frames are lists of columns). > > <SNIP> > > I never cease to be amazed by the sagacity and perspicacity of the > designers of R. I would have worried that x["V2"] would turn out to be > a *list* (of length 1), but no, it retains the data.frame class, which > is clearly the Right Thing To Do. > > cheers, > > Rolf > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Stats. Dep't. phone: +64-9-373-7599 ext. 89622 > Home phone: +64-9-480-4619 >[[alternative HTML version deleted]]
Jeff Newmiller
2023-Feb-13 02:51 UTC
[R] Removing variables from data frame with a wile card
Complain, complain... x[ names( x ) != "V2" ] or x[ ! names( x ) %in% c( "V2", "V3" ) ] or any other character or logical or integer expression that selects columns you want... On February 12, 2023 6:38:00 PM PST, Steven Yen <styen at ntu.edu.tw> wrote:>x[?V2?] would retain columns of x headed by V2. What I need is the opposite??I need a data grime with those columns excluded. > >Steven from iPhone > >> On Feb 13, 2023, at 9:33 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote: >> >> ? >>> On Sun, 12 Feb 2023 14:57:36 -0800 >>> Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: >>> >>> x["V2"] >>> >>> is more efficient than using drop=FALSE, and perfectly normal syntax >>> (data frames are lists of columns). >> >> <SNIP> >> >> I never cease to be amazed by the sagacity and perspicacity of the >> designers of R. I would have worried that x["V2"] would turn out to be >> a *list* (of length 1), but no, it retains the data.frame class, which >> is clearly the Right Thing To Do. >> >> cheers, >> >> Rolf >> >> -- >> Honorary Research Fellow >> Department of Statistics >> University of Auckland >> Stats. Dep't. phone: +64-9-373-7599 ext. 89622 >> Home phone: +64-9-480-4619 >>-- Sent from my phone. Please excuse my brevity.
Andrew Simmons
2023-Feb-13 02:52 UTC
[R] Removing variables from data frame with a wile card
What I meant is that that mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] and mydata[!grepl("^yr", colnames(mydata))] should be identical. Some people would prefer the first because the indexing looks the same as matrix indexing, whereas some people would prefer the second because it is more efficient. However, I would argue it is exactly as efficient. You can see from the first few lines of `[.data.frame` when the first index is missing and the second is provided, it does almost the same thing as if only the first index provided. On Sun, Feb 12, 2023 at 9:38 PM Steven Yen <styen at ntu.edu.tw> wrote:> > x[?V2?] would retain columns of x headed by V2. What I need is the opposite??I need a data grime with those columns excluded. > > Steven from iPhone > > On Feb 13, 2023, at 9:33 AM, Rolf Turner <r.turner at auckland.ac.nz> wrote: > > ? > On Sun, 12 Feb 2023 14:57:36 -0800 > Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > > x["V2"] > > > is more efficient than using drop=FALSE, and perfectly normal syntax > > (data frames are lists of columns). > > > <SNIP> > > I never cease to be amazed by the sagacity and perspicacity of the > designers of R. I would have worried that x["V2"] would turn out to be > a *list* (of length 1), but no, it retains the data.frame class, which > is clearly the Right Thing To Do. > > cheers, > > Rolf > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Stats. Dep't. phone: +64-9-373-7599 ext. 89622 > Home phone: +64-9-480-4619 >