jeudi 19 mars 2015

R: ifelse statement: comparing data.frames

I have 2 dataframes where im trying to compare the value in one with another If the value matches in both table 1 and 2, then a third value from table 2 is inserted into Table one.


Example Table My DF



words number
1 it 1
2 was 2
3 the 3
4 LTD QTY 4
5 end 5
6 of 6
7 winter 7


Table x.sub



lev_dist Var1 Var2
31 1 LTD QTY LTD QTY


What i want to say is, if Var1 in x.sub is equal to words in MyDF then insert x.sub.lev_dist in a third column next to the word in mydf


My attempt is below but keeps producing 3 in the results instead of the lev_value



mydf$lev_dist <- ifelse(test = (mydf$words == x.sub$Var1),x.sub$Var1,0)


Results:



words number lev_dist
1 it 1 0
2 was 2 0
3 the 3 0
4 LTD QTY 4 3
5 end 5 0
6 of 6 0
7 winter 7 0


Can anyone help


Aucun commentaire:

Enregistrer un commentaire