dede
dede

Reputation: 1171

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

Upvotes: 0

Views: 1100

Answers (4)

Valentin Ruano
Valentin Ruano

Reputation: 2809

Basic operations on R matrix-like data structures tend to be cell-wise. Logicals mixed with numbers in operations tend to coerced into the number themselves, 0 (FALSE) and 1 (TRUE) so the (A == B) + 0 would do what you want to cell-wise, however to make sure that the result is a data.frame and not a matrix you need to call as.data.frame:

C = as.data.frame((A == B) + 0)

Upvotes: 0

Katie
Katie

Reputation: 362

The previous answers are correct. If you really want to use an if statement, then you can use this:

C <- ifelse(A == B, 1, 0)

Upvotes: 0

KenHBS
KenHBS

Reputation: 7164

You assess whether A and B are the same (cell-wise) and then transform the TRUE / FALSE values into binary by multiplying it by 1:

df <- (A == B) * 1

Upvotes: 1

Andrew Brēza
Andrew Brēza

Reputation: 8317

Because of R's behind the scenes magic, you don't even need to use an if statement. You can just do this:

C <- (A == B) * 1

The first part (A == B) goes through every cell of A and B and compares them directly. The result is a bunch of TRUE and FALSE values. Multiplying everything by 1 forces the TRUE values to become 1 and FALSE to become 0.

Upvotes: 1

Related Questions