, 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
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 peopl
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
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 need
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 more
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 x[
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 >
mydata<-mydata[,!grepl("
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. List-sl
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 = FALSE
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:
>>
>>
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 <- ..
, 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 d
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 modify
n 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
>
>> On Jan 14, 2023, at 3:08 PM, Andre
p 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
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 <- ...
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
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
>
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", colna
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
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
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] "yea
22 matches
Mail list logo