Combining two columns with two values

I have columns that I know the name there and that their data is 0 and 1.

I would like to combine them into one, but if there is 1 in one line, take one value or if I have 1 and 1, save 1.

Sample data:

stockI stockII
    1   0
    1   0
    0   0
    0   0
    0   0
    0   0
    0   0
    1   0
    0   0
    1   1

The conclusion I could expect:

stockI/stockII
    0   
    1   
    0   
    0   
    0   
    0   
    0   
    0   
    0   
    1   

Is there any way cbindto do this?

+4
source share
1 answer

We can try

as.integer(with(df1, (c(FALSE,stockI[-1] &
   stockI[-nrow(df1)]) & stockI) | (stockI & stockII)))
#[1] 0 1 0 0 0 0 0 0 0 1
+3
source

All Articles