Say this is my data.
mydat=structure(list(ItemRelation = c(158200L, 158204L), DocumentNum = c(1715L, 1715L), CalendarYear = c(2018L, 2018L), X1 = c(0L, 0L), X2 = c(0L, 0L), X3 = c(0L, 0L), X4 = c(NA, NA), X5 = c(107L, 105L), X6 = c(NA, NA)), .Names = c("ItemRelation", "DocumentNum", "CalendarYear", "X1", "X2", "X3", "X4", "X5", "X6"), class = "data.frame", row.names = c(NA, -2L)) How can I create the condition that if X6=NA, then replace NA by value of X5?
In this example, the desired output would be:
ItemRelation DocumentNum CalendarYear X1 X2 X3 X4 X5 X6 1 158200 1715 2018 0 0 0 NA 107 107 2 158204 1715 2018 0 0 0 NA 105 105
with(mydat, ifelse(is.na(X6), X5, X6))ifelselikeifelse(is.na(X6), X5, X6)