Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread avi.e.gross
, February 12, 2023 5:19 PM To: Andrew Simmons Cc: R-help Mailing List Subject: Re: [R] Removing variables from data frame with a wile card In the line suggested by Andrew Simmons, mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] what does drop=FALSE do? Thanks. On 1/14

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Steven Yen
Great, Thanks. Now I have many options. Steven from iPhone > On Feb 13, 2023, at 10:52 AM, Andrew Simmons wrote: > > What I meant is that that > > mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] > > and > > mydata[!grepl("^yr", colnames(mydata))] > > should be identical. Some

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Andrew Simmons
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

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Jeff Newmiller
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 wrote: >x[“V2”] would retain columns of x headed by V2. What I

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Steven Yen
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 wrote: > >  >> On Sun, 12 Feb 2023 14:57:36 -0800 >> Jeff Newmiller wrote: >> >> x["V2"] >> >> is

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Rolf Turner
On Sun, 12 Feb 2023 14:57:36 -0800 Jeff Newmiller wrote: > x["V2"] > > is more efficient than using drop=FALSE, and perfectly normal syntax > (data frames are lists of columns). I never cease to be amazed by the sagacity and perspicacity of the designers of R. I would have worried that

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Steven T. Yen
Thanks Jeff and Andrew. My initial file, mydata, is a data frame with 92 columns (variables). After the operation (trimming), it remains a data frame with 72 variables. So yes indeed, I do not need the drop=FALSE. > is.data.frame(mydata) [1] TRUE > ncol(mydata) [1] 92 >

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Jeff Newmiller
x["V2"] is more efficient than using drop=FALSE, and perfectly normal syntax (data frames are lists of columns). I would ignore the naysayers, or put a comment in if you want to accelerate their uptake. As I understand it, one of the main reasons tibbles exist is because of drop=TRUE.

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Andrew Simmons
drop = FALSE means that should the indexing select exactly one column, then return a data frame with one column, instead of the object in the column. It's usually not necessary, but I've messed up some data before by assuming the indexing always returns a data frame when it doesn't, so drop =

Re: [R] Removing variables from data frame with a wile card

2023-02-12 Thread Steven T. Yen
In the line suggested by Andrew Simmons, mydata <- mydata[, !grepl("^yr", colnames(mydata)), drop = FALSE] what does drop=FALSE do? Thanks. On 1/14/2023 8:48 PM, Steven Yen wrote: > Thanks to all. Very helpful. > > Steven from iPhone > >> On Jan 14, 2023, at 3:08 PM, Andrew Simmons wrote: >>

Re: [R] Removing variables from data frame with a wile card

2023-01-15 Thread Rui Barradas
From: R-help on behalf of Valentin Petzel Sent: Saturday, January 14, 2023 1:21 PM To: avi.e.gr...@gmail.com Cc: 'R-help Mailing List' Subject: Re: [R] Removing variables from data frame with a wile card Hello Avi, while something like d$something <- ... may s

Re: [R] Removing variables from data frame with a wile card

2023-01-15 Thread avi.e.gross
, John Sent: Sunday, January 15, 2023 11:55 AM To: Valentin Petzel ; avi.e.gr...@gmail.com Cc: 'R-help Mailing List' Subject: Re: [R] Removing variables from data frame with a wile card I am new to this thread. At the risk of presenting something that has been shown before, below I demonstrate

Re: [R] Removing variables from data frame with a wile card

2023-01-15 Thread Sorkin, John
lp on behalf of Valentin Petzel Sent: Saturday, January 14, 2023 1:21 PM To: avi.e.gr...@gmail.com Cc: 'R-help Mailing List' Subject: Re: [R] Removing variables from data frame with a wile card Hello Avi, while something like d$something <- ... may seem like you're directly modifying the da

Re: [R] Removing variables from data frame with a wile card

2023-01-15 Thread Valentin Petzel
hange. > > For those who like to use the tidyverse, it comes with lots of tools that let > you select columns that start with or end with or contain some pattern and I > find that way easier. > > > > -Original Message- > From: R-help On Behalf Of Steven Yen >

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread avi.e.gross
help mailto:r-help-boun...@r-project.org> > On Behalf Of Steven Yen Sent: Saturday, January 14, 2023 7:49 AM To: Andrew Simmons mailto:akwsi...@gmail.com> > Cc: R-help Mailing List mailto:r-help@r-project.org> > Subject: Re: [R] Removing variables from data frame with a wile card Th

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread avi.e.gross
I prefer to avoid religious wars. -Original Message- From: Valentin Petzel Sent: Saturday, January 14, 2023 1:21 PM To: avi.e.gr...@gmail.com Cc: 'R-help Mailing List' Subject: Re: [R] Removing variables from data frame with a wile card Hello Avi, while something like d$something <- ... may

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread John Kane
ier. > > > > -Original Message- > From: R-help On Behalf Of Steven Yen > Sent: Saturday, January 14, 2023 7:49 AM > To: Andrew Simmons > Cc: R-help Mailing List > Subject: Re: [R] Removing variables from data frame with a wile card > > Thanks to all. Ver

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread avi.e.gross
find that way easier. -Original Message- From: R-help On Behalf Of Steven Yen Sent: Saturday, January 14, 2023 7:49 AM To: Andrew Simmons Cc: R-help Mailing List Subject: Re: [R] Removing variables from data frame with a wile card Thanks to all. Very helpful. Steven from iPhone >

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread Bill Dunlap
The -grep(pattern,colnames) as a subscript is a bit dangerous. If no colname matches the pattern then all columns will be omitted (because -0 is the same as 0, which means no column). !grepl(pattern,colnames) avoids this problem. > mydata <- data.frame(A=1:3,B=11:13) > mydata[, -grep("^yr",

Re: [R] Removing variables from data frame with a wile card

2023-01-14 Thread Steven Yen
Thanks to all. Very helpful. Steven from iPhone > On Jan 14, 2023, at 3:08 PM, Andrew Simmons wrote: > > You'll want to use grep() or grepl(). By default, grep() uses extended > regular expressions to find matches, but you can also use perl regular > expressions and globbing (after converting

Re: [R] Removing variables from data frame with a wile card

2023-01-13 Thread Andrew Simmons
You'll want to use grep() or grepl(). By default, grep() uses extended regular expressions to find matches, but you can also use perl regular expressions and globbing (after converting to a regular expression). For example: grepl("^yr", colnames(mydata)) will tell you which 'colnames' start with

Re: [R] Removing variables from data frame with a wile card

2023-01-13 Thread Eric Berger
mydata[, -grep("^yr",colnames(mydata))] On Sat, Jan 14, 2023 at 8:57 AM Steven T. Yen wrote: > I have a data frame containing variables "yr3",...,"yr28". > > How do I remove them with a wild cardsomething similar to "del yr*" > in Windows/doc? Thank you. > > > colnames(mydata) >[1]

[R] Removing variables from data frame with a wile card

2023-01-13 Thread Steven T. Yen
I have a data frame containing variables "yr3",...,"yr28". How do I remove them with a wild cardsomething similar to "del yr*" in Windows/doc? Thank you. > colnames(mydata)   [1] "year"   "weight" "confeduc"   "confothr" "college"   [6] ...  [41] "yr3"    "yr4"    "yr5"