R:ifelse语句:比较data.frames

时间:2015-03-19 13:03:27

标签: r if-statement dataframe

我有2个数据帧,我试图比较一个与另一个的值 如果值在表1和表2中都匹配,则表2中的第三个值将插入表1中。

示例表格我的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

表x.sub

   lev_dist    Var1    Var2
31        1 LTD QTY LTD QTY

我想说的是,如果x.sub中的Var1等于MyDF中的单词,则将x.sub.lev_dist插入mydf中单词旁边的第三列

我的尝试在下面,但在结果中不断产生3而不是lev_value

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

结果:

    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

任何人都可以提供帮助

2 个答案:

答案 0 :(得分:2)

x.sub$Var1factor列。因此,当我们执行ifelse时,我们得到因子的数字levels。将x.sub$Var1替换为as.character(x.sub$Var1)

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

如果列为character类,则可以避免这种情况。使用stringsAsFactors=FALSEread.csv/read.table中的data.frame可确保所有字符列均为character类。

答案 1 :(得分:2)

您还可以使用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