lundi 14 août 2017

Use if statement for each cell of a dataframe in R

I have two dataframes, A and B, each with 64 rows and 431 columns. Each dataframe contains values of zeros and ones. I need to create a new dataframe C that has values of 1 when the cell of A is equal to the cell of B, and a value of 0 when a cell of A is different to the cell of B. How to apply the if statement to each cell of the two dataframes?

Example of dataframes
A <- data.frame(replicate(431,sample(0:1,64,rep=TRUE)))
B <- data.frame(replicate(431,sample(0:1,64,rep=TRUE)))

Example rows from A
  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 0  1  1  0  1  0  1  0  0  1
2 1  1  0  1  1  0  0  0  0  0
3 1  0  0  0  1  0  0  1  1  0
4 0  0  0  0  1  1  1  1  1  0
5 1  0  1  1  0  0  0  1  1  1

Example rows from B
  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 1  0  1  0  0  1  0  1  0  1
2 0  0  0  1  0  1  1  1  1  1
3 1  0  1  1  1  1  0  0  0  0
4 1  0  0  0  0  1  1  0  0  0
5 0  0  0  0  1  1  1  1  1  0

Output I would like to obtain, dataframe C
  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 0  0  1  0  0  0  0  0  1  0
2 0  0  1  1  0  0  0  0  0  0
3 1  1  0  0  1  0  1  0  0  1
4 0  1  1  1  0  1  1  0  0  1
5 0  1  0  0  0  0  0  1  0  0

Aucun commentaire:

Enregistrer un commentaire