Library contents

R Logical vector subsetting

Retain the stock bins that still contain items to pick.

A simple example

Logical vector subsetting

  1. Start with 1, 2, 3
  2. Do this Remove values below 2
  3. Result 2, 3
The rule rejects only the first value. The surviving pair stays in order.

Syntax

R syntax
counts[counts != 0]
Run these examples in R

Save one example at a time as Lesson.R. Use R 4.x.

Rscript --vanilla Lesson.R

Clear empty stock bins from the pick list

A stockroom prints only bins that contain items a picker can collect. Empty bins appear twice in today's count, and both should disappear while the remaining quantities keep their order.

From the programavailable <- counts[!is.na(counts) & counts != 0L]
Result4, 2
Lesson.R
counts <- c(4L, 0L, 2L, 0L)
available <- counts[!is.na(counts) & counts != 0L]
cat(paste(available, collapse = ", "))
Output
4, 2

Both zeros are excluded and the positive counts stay in order.

Advertisements

Count packs on the delivery slip

A loading desk receives four item counts in sequence. Only positive quantities go to the packing desk. Which entries survive, in their original order?

Given6 · 2 · 6 · 0
Worked result6 · 2 · 6

Review print quantities

A print room records four job quantities, including a cancelled job. Only positive quantities go to the packing desk. Which entries survive, in their original order?

Given3 · 0 · 8 · 3
Worked result3 · 8 · 3

What to watch for

NA values in the condition propagate into the result. Decide how missing counts should be treated.

Documentation for Logical vector subsetting