When trying to replace values, “missing values are not allowed in subscripted assignments of data frames”

前端 未结 6 1596
日久生厌
日久生厌 2021-02-01 14:31

I have a table that has two columns: whether you were sick (H01) and the number of days sick (H03). However, the number of days sick is NA if H01 == false, and I would like to s

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 15:16

    You can use ifelse, like so

    pe94.person$foo <- ifelse(!is.na(pe94.person$H01) & pe94.person$H01 == 12, 0, pe94.person$H03)
    

    check if foo meets your criteria and then go ahead and assign it to pe94.person$H03 directly. I find it safer to assign it a new variable and usually use that in subsequent analysis.

提交回复
热议问题