[R] create

Val v@|kremk @end|ng |rom gm@||@com
Wed Jan 27 18:42:28 CET 2021


Hi all, I have a sample of data as shown below,

 dt <-read.table(text="name Item check
 A  DESK      NORF
 B  RANGE   GARRA
 C  CLOCK    PALM
 D  DESK      RR
 E  ALARM    DESPRF
 H  DESK       RF
 K  DESK      CORR
 K  WARF     CORR
 G  NONE      RF ",header=TRUE, fill=T)

I want create  another  column (flag2) and assign a value  0 or 1
if the check column values are  within  code2 list  and Item is DESK
then flag2 =1 otherwise 0

code2=c("RR","RF")
index2=grep(paste(code2,collapse="|"),dt$check)

dt$flag2=0
dt$flag2[index2]=1
How can I add the second condition?


Desired output  is  shown below
     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

Thank you,



More information about the R-help mailing list