Creating a new variable that accepts different columns from the existing one in the data frame, depending on the grouping variable

I had no idea where to start with this code. I want to bind a new variable to an existing data frame that accepts different columns depending on the grouping variable. For example, let's say I have columns

ABCDEF 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 add a new column "G", which is column B if A is 1 and column D if A is 2

  ABCDEFG 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

+4
source share
2 answers

Here are a few 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 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) # 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!

+9
source

Assuming your data.frame is called "mydf", you can use ifelse :

 within(mydf, { G <- ifelse(A == 1, B, ifelse(A == 2, D, 0)) }) # ABCDEFG # 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 
+5
source

All Articles