R: ifelse statement: comparing data.frames

点点圈 提交于 2020-01-05 17:57:23

问题


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


回答1:


The x.sub$Var1 is a factor column. So, when we do the ifelse, we get the numeric levels of the factor. Replace x.sub$Var1 with as.character(x.sub$Var1) in the ifelse

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

This could have avoided if the columns were of character class. Using stringsAsFactors=FALSE in the read.csv/read.table or data.frame would ensure that all the character columns are of character class.




回答2:


You can also use merge:

x.sub = setNames(x.sub,c('lev_dist','words','Var2'))

df_ = merge(df, x.sub[,1:2], by='words', all=T)
df_[is.na(df_)]=0

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


来源:https://stackoverflow.com/questions/29145342/r-ifelse-statement-comparing-data-frames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!