Search code examples
rdataframereplacerecode

R. Conditional replace of characters in data frame if two columns match


I have a data frame with columns c1 to c11, which looks like this:

c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11  
G A G 1 1 0 1 1 0 0 1
T C T 0 0 1 1 0 1 0 1
C C T 0 1 1 1 1 1 1 0

I would like to do the following: if the character in c1 is the same as c3, replace, from c4 to c11, 1s by 2s and 0s by 3s. Otherwise, replace 1s by 3s and 0s by 2s

At the end I would get this data frame:

c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11  
G A G 2 2 3 2 2 3 3 2
T C T 3 3 2 2 3 2 3 2
C C T 2 3 3 3 3 3 3 2

Solution

  • Try the following. It uses nested ifelse and an index vector. Maybe there are simpler ways, but this one only uses base R.

    fun <- function(x){
        ifelse(inx,
            ifelse(x == 1, 2, 3),
            ifelse(x == 1, 3, 2)
        )
    }
    
    inx <- as.character(data$c1) == as.character(data$c3)
    data[4:11]  <- lapply(data[4:11], fun)