R:ifelse语句:比较data.frames [英] R: ifelse statement: comparing data.frames

查看:130
本文介绍了R:ifelse语句:比较data.frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据框,其中im尝试将其中的值与另一个
进行比较。如果值在表1和2中匹配,则表2中的第三个值被插入到表1中。



示例表我的DF

 字数
1 it 1
2为2
3 3
4 LTD数量4
5结束5
6 6 6
7冬天7

表x.sub

  lev_dist Var1 Var2 
31 1 LTD数量有限公司数量

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



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

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

结果:

 字数lev_dist 
1 it 1 0
2为2 0
3 3 0
4 LTD数量4 3
5 zh d 5 0
6/6 0
7冬季7 0

帮助

解决方案

x.sub $ Var1 是一个 factor 列。所以,当我们执行 ifelse 时,我们得到这个因子的数值级别。在 x.sub $ Var1 与 as.character(x.sub $ Var1) > ifelse

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

如果列是字符类,可以避免。使用 stringsAsFactors = FALSE read.csv / read.table data.frame 将确保所有字符列都是字符类。


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

解决方案

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.

这篇关于R:ifelse语句:比较data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆