Eracog
Eracog

Reputation: 225

Merging two columns with two values

I have columns which I know there name and that their data are 0 and 1.

I would like to merge them to one but if in one row exist the 1 take the one value or if I have 1 and 1 keep 1.

Example of data:

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

the output I could expect:

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

Is there any cbind method to make it?

Upvotes: 4

Views: 57

Answers (1)

akrun
akrun

Reputation: 887951

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

Upvotes: 3

Related Questions