Apply if-else function row-wise

PeterQ

I have the below matrix:

structure(c("G", "G", "A", "C", "G", "G", "A", "A", "G", "A", 
"A", "A", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "0", "0", 
"0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "1", "0", "0", 
"1", "0", "0", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0", 
"0", "0", "0", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0", 
"1", "1", "0", "0", "0"), .Dim = c(6L, 22L), .Dimnames = list(
c("1", "2", "3", "4", "5", "6"), c("allele1", "allele2", 
"s1a", "s1b", "s2a", "s2b", "s3a", "s3b", "s4a", "s4b", "s5a", 
"s5b", "s6a", "s6b", "s7a", "s7b", "s8a", "s8b", "s9a", "s9b", 
"s10a", "s10b")))

Which looks like this:

  allele1 allele2 s1a s1b s2a s2b s3a s3b s4a s4b s5a s5b s6a s6b s7a s7b s8a s8b s9a s9b s10a s10b
1 "G"     "A"     "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "1" "0" "0"  "0" 
2 "G"     "A"     "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1"  "1" 
3 "A"     "G"     "1" "0" "1" "0" "0" "1" "1" "0" "0" "1" "0" "1" "0" "0" "1" "1" "1" "0" "1"  "1" 
4 "C"     "A"     "0" "0" "0" "0" "1" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "1" "0"  "0" 
5 "G"     "A"     "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "1" "0" "0"  "0" 
6 "G"     "A"     "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0"  "0"

How could I look in each row and replace the occurrences of 0 with the value in column allele1, and the occurrences of 1 with the value in column allele2?

Rich Scriven

You could do straight logical replacement. It takes a few lines of code, but would work fine. The idea is to replicate the first and second columns for the number of columns we are replacing. We can do that with e.g. m[, c(1, 1, 1, 1)], replicating the first column four times. This uses no loops.

## find the zeros
is0 <- m[, -(1:2)] == 0
## replace the values by replicating the relevant columns
## then applying the logical subset
m[, -(1:2)][is0] <- m[, rep(1, ncol(m)-2)][is0]
m[, -(1:2)][!is0] <- m[, rep(2, ncol(m)-2)][!is0]

resulting in the modified m

m
#   allele1 allele2 s1a s1b s2a s2b s3a s3b s4a s4b s5a s5b s6a s6b s7a s7b s8a s8b s9a s9b s10a s10b
# 1 "G"     "A"     "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A" "G" "G" "G" "G" "A" "G" "G"  "G" 
# 2 "G"     "A"     "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A"  "A" 
# 3 "A"     "G"     "G" "A" "G" "A" "A" "G" "G" "A" "A" "G" "A" "G" "A" "A" "G" "G" "G" "A" "G"  "G" 
# 4 "C"     "A"     "C" "C" "C" "C" "A" "C" "C" "C" "C" "C" "C" "C" "C" "A" "C" "C" "C" "A" "C"  "C" 
# 5 "G"     "A"     "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A" "G" "G" "G" "G" "A" "G" "G"  "G" 
# 6 "G"     "A"     "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A" "G" "G" "G" "G"  "G" 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

From Java

getting the index of a row in a pandas apply function

From Dev

Apply a function to each row of a ndarray

From Dev

Apply numpy nonzero row-wise?

From Dev

Applying a function row-wise for a dataset

From Dev

Conditionally apply a function on a row

From Dev

How to apply max function for each row in KDB?

From Dev

How to apply xts function per row in a dataframe

From Dev

Apply function row wise on pandas data frame on columns with numerical values

From Dev

create scoring function and apply to each row in R

From Dev

Pandas: How to structure row-wise apply which requires previous output as input

From Dev

Apply a column wise function in a list of data.tables

From Dev

How to apply a function on every row on a dataframe?

From Dev

pandas - apply replace function with condition row-wise

From Dev

apply a function in data frame with if/else statement in R

From Dev

PySpark row-wise function composition

From Dev

apply/ map function to prior row

From Dev

Apply function on each row (row-wise) of a NumPy array

From Dev

apply a function to each row of the dataframe

From Dev

How to control the row/column-wise printing of plots when using apply functions

From Dev

Apply a function separately to each row

From Dev

Apply a function to each row of a ndarray

From Dev

Apply function and create new row

From Dev

apply a function in data frame with if/else statement in R

From Dev

apply/ map function to prior row

From Dev

Is it possible to realize column-wise operation and row-wise operation by one template function?

From Dev

Apply a function row-wise to a data.table

From Dev

Apply FUN row-wise on data frame with integer and character variables

From Dev

PySpark Row-wise Function

Related Related

  1. 1

    pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

  2. 2

    getting the index of a row in a pandas apply function

  3. 3

    Apply a function to each row of a ndarray

  4. 4

    Apply numpy nonzero row-wise?

  5. 5

    Applying a function row-wise for a dataset

  6. 6

    Conditionally apply a function on a row

  7. 7

    How to apply max function for each row in KDB?

  8. 8

    How to apply xts function per row in a dataframe

  9. 9

    Apply function row wise on pandas data frame on columns with numerical values

  10. 10

    create scoring function and apply to each row in R

  11. 11

    Pandas: How to structure row-wise apply which requires previous output as input

  12. 12

    Apply a column wise function in a list of data.tables

  13. 13

    How to apply a function on every row on a dataframe?

  14. 14

    pandas - apply replace function with condition row-wise

  15. 15

    apply a function in data frame with if/else statement in R

  16. 16

    PySpark row-wise function composition

  17. 17

    apply/ map function to prior row

  18. 18

    Apply function on each row (row-wise) of a NumPy array

  19. 19

    apply a function to each row of the dataframe

  20. 20

    How to control the row/column-wise printing of plots when using apply functions

  21. 21

    Apply a function separately to each row

  22. 22

    Apply a function to each row of a ndarray

  23. 23

    Apply function and create new row

  24. 24

    apply a function in data frame with if/else statement in R

  25. 25

    apply/ map function to prior row

  26. 26

    Is it possible to realize column-wise operation and row-wise operation by one template function?

  27. 27

    Apply a function row-wise to a data.table

  28. 28

    Apply FUN row-wise on data frame with integer and character variables

  29. 29

    PySpark Row-wise Function

HotTag

Archive