I am trying to add an extra column onto a database df2
based on information from another database df1
. I have one column called sequence_annotation
in df2
if this column has a numerical value then I want it to copy the information in a column called PoleX
and AnX
for characters. The column/identifier that is constant for both databases is called CTsite
so this is how I am matching up which values from PoleX
or AnX
should be added.
df2 looks like this:
CTsite sequence_annotation
OCCAJ01 Lt
OCCAJ01 20
OCCAJ04 Mt
OCCAJ04 40
df1 looks like this:
CTsite AnX Pole X
OCCAJ01 720 2592
OCCAJ04 640 3264
The disired output is:
CTsite sequence_annotation xres
OCCAJ01 Lt 720
OCCAJ01 20 2592
OCCAJ04 Mt 640
OCCAJ04 40 3264
I have tried to create a code base on previous questions and comments but it doesn't work. Would be very grateful if anyone can help. This is the code:
if (mode(df2$sequence_annotation)=="numeric") {
df2$xres <- df1$PoleX[match(df1$CTsite,df2$CTsite)]
} else {
df2$xres <- df1$AnX[match(df1$CTsite, df2$CTsite)]
}