该怎样用R语言来根据逻辑条件来创建新的变量

现在有一列名叫race的数列,数列里面是b和w,现在想建立一列新数据为treatment,如果race是b,则为1;如果race为w,则为0

在r中引用数据框中的变量要用$,如获取race,就用hwk3$race。

if(race==b) treatment<- as.numeric(1)
else if(race==w) treatment<- as.numeric(0)

img

题目描述是这样的

hwk3<-list(hwk3,if(race==b) treatment<- as.numeric(1)

  •        else if(race==w) treatment<- as.numeric(0))
    
    各位大哥看看怎么错了,我上面报错说找不到race