简体   繁体   中英

How to create dummy variable by subsetting with two conditions

I would like to create a dummy variable that equals one if the person had at least one doctor visit after the lottery.

The variables look as such:

lottery consists of " Not selected " and " selected " doc_visit_num consists of numerical values ranging from 0-30

So far I have: PS1$visited_doctor_lottery <- subset(PS1, lottery == "Selected" | doc_visit_num >= 1)

Can someone please help me create a dummy variable with two conditions. Thank you.

也许你可以试试下面的代码

within(PS1, visited_doctor_lottery <- + (lottery == "Selected" & doc_visit_num >= 1))

Does this answer:

> PS1
        lottery doc_visit_num
1      Selected             1
2      Selected             0
3      Selected            23
4  Not Selected            25
5      Selected            13
6  Not Selected            24
7  Not Selected             1
8  Not Selected             9
9  Not Selected            10
10 Not Selected            23

> PS1 %>% mutate(visited_doctor_lottery = case_when(
+                                 lottery == 'Selected' & doc_visit_num >= 1 ~ 1,
+                                 TRUE ~ 0
+                                           ))
        lottery doc_visit_num visited_doctor_lottery
1      Selected             1                      1
2      Selected             0                      0
3      Selected            23                      1
4  Not Selected            25                      0
5      Selected            13                      1
6  Not Selected            24                      0
7  Not Selected             1                      0
8  Not Selected             9                      0
9  Not Selected            10                      0
10 Not Selected            23                      0

We can also use transform:

> transform(PS1, visited_doctor_lottery = +(lottery == 'Selected' & doc_visit_num >= 1))
        lottery doc_visit_num visited_doctor_lottery
1      Selected             1                      1
2      Selected             0                      0
3      Selected            23                      1
4  Not Selected            25                      0
5      Selected            13                      1
6  Not Selected            24                      0
7  Not Selected             1                      0
8  Not Selected             9                      0
9  Not Selected            10                      0
10 Not Selected            23                      0

Data used:

> dput(PS1)
structure(list(lottery = structure(c(2L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L), .Label = c("Not Selected", "Selected"), class = "factor"), 
    doc_visit_num = c(1, 0, 23, 25, 13, 24, 1, 9, 10, 23)), row.names = c(NA, 
-10L), class = "data.frame")
> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM