Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Selecting items in a vector

30 views
Skip to first unread message

WJ

unread,
Jul 21, 2016, 2:05:25 PM7/21/16
to
R:

Create a vector of random integers:

> sample(-3:5, 22, rep=TRUE) -> v
> v
[1] 5 2 -3 2 4 -1 -2 -3 -3 -1 4 4 -1 0 0 -2 -2 1 2 0 0 3

Find the indices at which v < 0:

> which(v < 0)
[1] 3 6 7 8 9 10 13 16 17

Extract the negative numbers:

> v[which(v < 0)]
[1] -3 -1 -2 -3 -3 -1 -1 -2 -2

How much easier is it in ANS Forth?

--
After being told there was no money, the attackers tore off the woman's clothes
and raped her until five others arrived, according to the documents. The new
arrivals took turns having sex with her and then sodomized her.... At gunpoint,
the assailants forced the mother and son to have sex.
www.sun-sentinel.com/local/palm-beach/sfl-flpdunbar0822nbaug22-story.html

WJ

unread,
Jul 21, 2016, 2:11:58 PM7/21/16
to
WJ wrote:

> R:
>
> Create a vector of random integers:
>
> > sample(-3:5, 22, rep=TRUE) -> v
> > v
> [1] 5 2 -3 2 4 -1 -2 -3 -3 -1 4 4 -1 0 0 -2 -2 1 2 0 0 3
>
> Find the indices at which v < 0:
>
> > which(v < 0)
> [1] 3 6 7 8 9 10 13 16 17
>
> Extract the negative numbers:
>
> > v[which(v < 0)]
> [1] -3 -1 -2 -3 -3 -1 -1 -2 -2
>
> How much easier is it in ANS Forth?

That was regrettable newbie prolixity.

> v<0
[1] FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
[13] TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
> v[v<0]
[1] -3 -1 -2 -3 -3 -1 -1 -2 -2

--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html

WJ

unread,
Jul 21, 2016, 4:44:50 PM7/21/16
to
WJ wrote:

> WJ wrote:
>
> > R:
> >
> > Create a vector of random integers:
> >
> > > sample(-3:5, 22, rep=TRUE) -> v
> > > v
> > [1] 5 2 -3 2 4 -1 -2 -3 -3 -1 4 4 -1 0 0 -2 -2 1 2 0 0 3
> >
> > Find the indices at which v < 0:
> >
> > > which(v < 0)
> > [1] 3 6 7 8 9 10 13 16 17
> >
> > Extract the negative numbers:
> >
> > > v[which(v < 0)]
> > [1] -3 -1 -2 -3 -3 -1 -1 -2 -2
> >
> > How much easier is it in ANS Forth?
>
> That was regrettable newbie prolixity.
>
> > v<0
> [1] FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
> [13] TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
> > v[v<0]
> [1] -3 -1 -2 -3 -3 -1 -1 -2 -2

Change all values < 0 to 90:

> v[v<0] = 90
> v
[1] 5 2 90 2 4 90 90 90 90 90 4 4 90 0 0 90 90 1 2 0 0 3
0 new messages