Use if statement for each cell of a dataframe in R -


i have 2 dataframes, a , b, each 64 rows , 431 columns. each dataframe contains values of zeros , ones. need create new dataframe c has values of 1 when cell of a equal cell of b, , value of 0 when cell of a different cell of b. how apply if statement each cell of 2 dataframes?

example of dataframes <- data.frame(replicate(431,sample(0:1,64,rep=true))) b <- data.frame(replicate(431,sample(0:1,64,rep=true)))  example rows   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 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 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 

because of r's behind scenes magic, don't need use if statement. can this:

c <- (a == b) * 1 

the first part (a == b) goes through every cell of , b , compares them directly. result bunch of true , false values. multiplying 1 forces true values become 1 , false become 0.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -