I am trying to create a table with the headings study_id, Conflict and create a third and forth column which contains data that corresponds to the value in column: Conflict
The code below achieves this aim. However it is rather long especially as I wish to expand this to cover several hundred different values in column: conflict
Thank you in advance for any pointers
df <- data.frame(study_id=c("1", "1", "4", "4", "5"),
Conflict=c("WATER.START", "WATER.STOP", "OIL.START", "NA", "WATER.STOP"),
Result=c("TRUE", "TRUE", "TRUE", "NA", "TRUE"))
df2 <- data.frame(study_id=c("1", "2", "3", "4", "5"),
WATER.start=c(1, 1, 2, NA, 6),
WATER.truestart=c(1, 1, 2, NA, 25),
WATER.stop=c(33, 3, 2, NA, 8),
WATER.truestop= c(34, 4, 2, NA, 8))
final <- left_join(df, df2, by ='study_id')
dd <- final %>% filter(Result == "TRUE" & Conflict == "WATER.START")
dd <- dd %>% subset(., Conflict == "WATER.START",
select=c(study_id, Conflict, WATER.start, WATER.truestart))
dd <- dd %>% rename(initial=WATER.start) %>% rename(verification=WATER.truestart)
ee <- final %>% filter(Result == "TRUE" & Conflict == "WATER.STOP")
ee <- ee %>% subset(., Conflict == "WATER.STOP",
select=c(study_id, Conflict, WATER.stop, WATER.truestop))
ee <- ee %>% rename(initial=WATER.stop) %>% rename(verification=WATER.truestop)
ff <- bind_rows(dd, ee)
gg <- ff %>% select(study_id, Conflict, initial, verification)
gg
# study_id Conflict initial verification
# 1 1 WATER.START 1 1
# 2 1 WATER.STOP 33 34
# 3 5 WATER.STOP 8 8