Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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

like image 957
luke123 Avatar asked Dec 08 '22 17:12

luke123


2 Answers

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!

like image 146
mnel Avatar answered Dec 11 '22 09:12

mnel


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
like image 34
A5C1D2H2I1M1N2O1R2T1 Avatar answered Dec 11 '22 08:12

A5C1D2H2I1M1N2O1R2T1