Q:

Creating a new variable which takes different columns from an existing on data frame, dependent on a grouping variable

I had no idea where to start with this code. I want to attach a new variable to an existing data frame which takes different columns depending on a grouping variable. For example, say I have columns
    A  B  C  D  E  F
    1  2  3  6  11 12
    1  7  5  10 8  9
    2  19 2  4  5  6
    2  8  4  3  1  1

I want to attach a new column "G" which is column B if A is 1 and column D if A is 2
    A  B  C  D  E  F   G
    1  2  3  6  11 12  2 
    1  7  5  10 8  9   7
    2  19 2  4  5  6   4
    2  8  4  3  1  1   3

thanks

A:

Here are a couple of options.
assuming your data.frame is called DF
Basic [ and indexing
# make everything in G =  B
DF$G <- DF$B
# replace those cases where A==2 with D
DF$G[DF$A==2] <- DF$D[DT$A==2]

using ifelse
Only one ifelse statement is required as 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)
# create a data.table with A as the key

DT <- data.table(DF, key = 'A')
# where the key (A) == 1 ], then assign G = B
DT[.(1), G := B]
# and where the key (A) == 2, then assign G = D
DT[.(2), G := D]

beautifully elegant!

A:

Assuming your data.frame is called "mydf", you can use ifelse:
within(mydf, {
  G <- ifelse(A == 1, B,
              ifelse(A == 2, D, 
                     0))
})
#   A  B C  D  E  F G
# 1 1  2 3  6 11 12 2
# 2 1  7 5 10  8  9 7
# 3 2 19 2  4  5  6 4
# 4 2  8 4  3  1  1 3

