Here is a sample of my data:
df<-read.table(text=" Colour Leave Real
Blue Y Yellow
Red N NA
Yellow Y NA
Green Y Green
Blue N NA
Green Y Red
Yellow Y Blue
Green Y NA
",header=TRUE)
I want to get this output:
Colour1 Colour2
FALSE Yellow
Red FALSE
Blue Yellow
FALSE Green
Blue FALSE
Red FALSE
Blue FALSE
Red Green
Logic Colour1 only gets Blue and Red, and colour 2 only gets Yellow and Green. Blue Corresponds to Yellow and Red Corresponds to Green. If Leave is “Y”, we look at Real, If Real has a colour, and we choose this colour. For example, Row1, colour= Blue, Leave=Y and Real=Yellow. So we select Yellow in colour2 and Colour 1 gets FALSE. If Leave is “N”, we look at Real, If Real does not have a colour, i.e., NA, we choose the colour in the Colour Column. For example, Row2, colour= Red, Leave=N and Real=NA. So we select Red in colour1 and Colour 2 gets FALSE. If Leave is “Y”, we look at Real, If Real does not have a colour, i.e., NA, we choose both colours. For example, Last row, colour= Green, Leave=Y and Real=NA. So we select Red in colour1 and Colour 2 gets Green.
if
andelse
statement inR
? You should additionally useis.na()
and%in%
functions in R as well. Please put your effort on firstly and ask exactly the point you get stuck. – maydin