Skip to content
Prev 387020 / 398502 Next

create

Thanks for the reprex. I think this is one way to do what you want:
dt$flag2 <- 0 + with(dt,Item == "DESK" & check %in% code2)
name  Item  check flag2
1    A  DESK   NORF     0
2    B RANGE  GARRA     0
3    C CLOCK   PALM     0
4    D  DESK     RR     1
5    E ALARM DESPRF     0
6    H  DESK     RF     1
7    K  DESK   CORR     0
8    K  WARF   CORR     0
9    G  NONE     RF     0

This uses:
1) logical coerced to numeric by 0 + ... construction , which is probably
unnecessary: leave it as logical
2) ?"%in%"
3) ?with

The first two are fairly basic and should be covered in most decent basic R
tutorials. Spending some time with one or more will probably save you a lot
of time and aggravation later. The with() function is somewhat more
advanced, but can save lazy people (= me) a lot of typing hassle.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Jan 27, 2021 at 9:48 AM Val <valkremk at gmail.com> wrote: