Here are a few options.
Assuming your data.frame is called DF
Basic [ and Indexing
# make everything in G = B DF$G <- DF$B
using ifelse
Only one ifelse statement is required since A is either 1 or 2
DF$G <- ifelse(DF$A==2, DF$D, DF$B)
using a data table.
I like data.table, for memory efficiency and coding elegance.
library(data.table)
beautifully elegant!
source share