Memra

Position, range, drop, condition — and R is 1-based

◈ 10 cards

amount4[2] is 180, amount4[2:3] is 180 760, amount4[-1] drops the first, amount4[amount4 > 300] is 420 760 — and prov4[amount4 > 300] indexes one vector by a condition on another. R counts from 1; Python from 0.

Square brackets pick elements

A spreadsheet reaches a value by its cell address, D3. R reaches an element of a vector by its position in square brackets, and positions start at 1:

> amount4 <- c(420, 180, 760, 250)
> amount4[2]
[1] 180
> amount4[1]
[1] 420

Worked example — four ways to index one vector

A range. 2:3 is the vector 2 3, so amount4[2:3] picks positions 2 and 3:

> amount4[2:3]
[1] 180 760

Any vector of positions works: amount4[c(1, 4)] is 420 250.

A negative index drops. -1 means everything except position 1:

> amount4[-1]
[1] 180 760 250

It does not mean the last element (that is amount4[length(amount4)]), and it does not reverse.

A logical condition. amount4 > 300 is itself a vector — four TRUE/FALSE values, one per element — and indexing by it keeps the TRUE positions:

> amount4 > 300
[1]  TRUE FALSE  TRUE FALSE
> amount4[amount4 > 300]
[1] 420 760

That is Module 6's filter, in one expression. The logical vector must be the same length as the one it indexes — one decision per element. The result is the elements that passed, not their positions; the positions are which(amount4 > 300), which prints [1] 1 3.

One vector indexed by a condition on another. The provinces of those four orders are prov4 <- c("ON", "QC", "ON", "BC"). Which provinces placed the orders over 300?

> prov4[amount4 > 300]
[1] "ON" "ON"

The condition is evaluated on amount4, the brackets are on prov4, and the two line up position by position because they are the same four orders. This is the pattern Module 11 uses on every data-frame column.

And the sum of the orders over 300 is sum(amount4[amount4 > 300])SUMIF, in R — which the numeric item asks for.

1-based against 0-based

R counts positions from 1, like a spreadsheet's row numbers. Python counts from 0: amount4[1] in Python is the second element, 180, and amount4[0] is 420. The code block below prints Python's amount4[1] — read its output against R's [1] 180 above and notice that the two agree for different reasons. An index past the end is NA in R (amount4[5]) and an error in Python.

Type three lines, then read a sum

Type the range, the condition, and the cross-vector index. The numeric asks for sum(amount4[amount4 > 300]).

CRISP-DM: indexing by condition is data preparation → select data — the filter, before there is a data frame to filter.

1234[2] and [2:3]420180760250[2] → 180[2:3] → 180 760[-1]420180760250kept: 180 760 250[amount4 > 300]420180760250TRUETRUEwhich(amount4 > 300) gives the positions 1 3; the bracket gives the elements 420 760.
Four indexes on one vector. [2] points at one position; [2:3] spans a range; [-1] drops the muted cell; [amount4 > 300] keeps the highlighted ones — 420 and 760, the elements, not the positions.
NORMAL ~/memra/learn/afm-112/indexing-a-vector utf-8 LF