RStudio 1.1.453 - Windows 10 - How to subset named vector by names that are NOT "foo"

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

RStudio 1.1.453 - Windows 10 - How to subset named vector by names that are NOT "foo"

إبراهيم خطاب Ibrauheem Khat'taub
H
i All,

If I have this vector:

> vect <- c(foo = 11, bar = 2, norf = 45)

I can have a subset that has only "bar and "norf" this way:
> vect[c("bar","norf")]

Now how do I achieve the same by asking it for a subset that simply
excludes "foo"? I tried all these, resulting in errors:

vect[-"foo"]
vect[-c("foo")]
vect[!"foo"]
vect[!c("foo")]

Thanks!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: RStudio 1.1.453 - Windows 10 - How to subset named vector by names that are NOT "foo"

Huzefa Khalil
try:

vect[which(names(vect) != "foo")]

On Tue, Jul 31, 2018 at 1:41 PM, إبراهيم خطاب Ibrauheem Khat'taub
<[hidden email]> wrote:

> H
> i All,
>
> If I have this vector:
>
>> vect <- c(foo = 11, bar = 2, norf = 45)
>
> I can have a subset that has only "bar and "norf" this way:
>> vect[c("bar","norf")]
>
> Now how do I achieve the same by asking it for a subset that simply
> excludes "foo"? I tried all these, resulting in errors:
>
> vect[-"foo"]
> vect[-c("foo")]
> vect[!"foo"]
> vect[!c("foo")]
>
> Thanks!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: RStudio 1.1.453 - Windows 10 - How to subset named vector by names that are NOT "foo"

Sarah Goslee
In reply to this post by إبراهيم خطاب Ibrauheem Khat'taub
Hi,

You need to tell R to look in the names component of your vector. Here
are three different ways:

vect <- c(foo = 11, bar = 2, norf = 45)

vect[!(names(vect) %in% c("foo"))] # easily generalizable to a longer list

vect[!grepl("foo", names(vect))]

vect[!(names(vect) == "foo")]

There are many more ways to do this, all predicated on matching a
string within the character vector containing the names of your
object.

Also, on this list we don't care at all if you're using R Studio, or
what the version is. We do potentially care what version of R itself
you are using.

Sarah

On Tue, Jul 31, 2018 at 1:41 PM, إبراهيم خطاب Ibrauheem Khat'taub
<[hidden email]> wrote:

> H
> i All,
>
> If I have this vector:
>
>> vect <- c(foo = 11, bar = 2, norf = 45)
>
> I can have a subset that has only "bar and "norf" this way:
>> vect[c("bar","norf")]
>
> Now how do I achieve the same by asking it for a subset that simply
> excludes "foo"? I tried all these, resulting in errors:
>
> vect[-"foo"]
> vect[-c("foo")]
> vect[!"foo"]
> vect[!c("foo")]
>
> Thanks!
>


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: RStudio 1.1.453 - Windows 10 - How to subset named vector by names that are NOT "foo"

إبراهيم خطاب Ibrauheem Khat'taub
Awesome, Sarah, thanks!

And thanks for the clarification about declaring the version of R.

Best,
Ibrahim

On Tue, 31 Jul 2018 at 13:50, Sarah Goslee <[hidden email]> wrote:

> Hi,
>
> You need to tell R to look in the names component of your vector. Here
> are three different ways:
>
> vect <- c(foo = 11, bar = 2, norf = 45)
>
> vect[!(names(vect) %in% c("foo"))] # easily generalizable to a longer list
>
> vect[!grepl("foo", names(vect))]
>
> vect[!(names(vect) == "foo")]
>
> There are many more ways to do this, all predicated on matching a
> string within the character vector containing the names of your
> object.
>
> Also, on this list we don't care at all if you're using R Studio, or
> what the version is. We do potentially care what version of R itself
> you are using.
>
> Sarah
>
> On Tue, Jul 31, 2018 at 1:41 PM, إبراهيم خطاب Ibrauheem Khat'taub
> <[hidden email]> wrote:
> > H
> > i All,
> >
> > If I have this vector:
> >
> >> vect <- c(foo = 11, bar = 2, norf = 45)
> >
> > I can have a subset that has only "bar and "norf" this way:
> >> vect[c("bar","norf")]
> >
> > Now how do I achieve the same by asking it for a subset that simply
> > excludes "foo"? I tried all these, resulting in errors:
> >
> > vect[-"foo"]
> > vect[-c("foo")]
> > vect[!"foo"]
> > vect[!c("foo")]
> >
> > Thanks!
> >
>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: RStudio 1.1.453 - Windows 10 - How to subset named vector by names that are NOT "foo"

Data Science Classes
In reply to this post by إبراهيم خطاب Ibrauheem Khat'taub
You can do

Vect[-grep (“foo”, names(vect))]

On Tue, 31 Jul 2018 at 11:12 PM, إبراهيم خطاب Ibrauheem Khat'taub <
[hidden email]> wrote:

> H
> i All,
>
> If I have this vector:
>
> > vect <- c(foo = 11, bar = 2, norf = 45)
>
> I can have a subset that has only "bar and "norf" this way:
> > vect[c("bar","norf")]
>
> Now how do I achieve the same by asking it for a subset that simply
> excludes "foo"? I tried all these, resulting in errors:
>
> vect[-"foo"]
> vect[-c("foo")]
> vect[!"foo"]
> vect[!c("foo")]
>
> Thanks!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
--
Regards,
Data Science Classes

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: RStudio 1.1.453 - Windows 10 - How to subset named vector by names that are NOT "foo"

إبراهيم خطاب Ibrauheem Khat'taub
Awesome, thanks!
مع خالص الشكر والسلام عليكم.
إبراهيم خطاب


On Tue, 31 Jul 2018 at 13:54, Data Science Classes <
[hidden email]> wrote:

> You can do
>
> Vect[-grep (“foo”, names(vect))]
>
> On Tue, 31 Jul 2018 at 11:12 PM, إبراهيم خطاب Ibrauheem Khat'taub <
> [hidden email]> wrote:
>
>> H
>> i All,
>>
>> If I have this vector:
>>
>> > vect <- c(foo = 11, bar = 2, norf = 45)
>>
>> I can have a subset that has only "bar and "norf" this way:
>> > vect[c("bar","norf")]
>>
>> Now how do I achieve the same by asking it for a subset that simply
>> excludes "foo"? I tried all these, resulting in errors:
>>
>> vect[-"foo"]
>> vect[-c("foo")]
>> vect[!"foo"]
>> vect[!c("foo")]
>>
>> Thanks!
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
> --
> Regards,
> Data Science Classes
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.