如果功能在R [英] if function in R

查看:107
本文介绍了如果功能在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中很新。我有两组数据。
当差值超过某个阈值时,我将它们标记为异常值。
现在,我想创建一个函数,从Table_2中减去Table_1列的行,其中标签是Result列中的outlier。



  errorr < -  data.frame(Table_1 = unname(x.fore $ pred),Table_2 = unname(rn25_29_t $ ambtemp ))
errorr< - transform(errorr,Result = ifelse(Table_1 -0.3< Table_2& Table_2< Table_1 + 0.3,'Normal','Outlier'))



$ b

示例数据:

  Table_1 Table_2结果
1 5.778986 5.58正常
2 5.768515 6.50异常值
3 5.758068 5.83正常
4 5.747644 5.54正常
5 5.737245 5.80正常
6 5.726869 6.03异常值

预期结果:

  5.768515  -  6.50 = -0.731485 
5.726869 - 6.03 = -0.303131


解决方案

另外,也许(虽然输出不那么漂亮)

  diff(t(dat [dat $ result ==Outlier,2:1]))
## 2 6
## Table_1 -0.731485 -0.303131


I am quite new in R. I have two set of data. I have labeled them as "outlier" when the difference exceeds a certain threshold. Now, I would like to make a function which subtract the row of Table_1 column from Table_2 where label is "outlier" in the Result column.

Code:

errorr <- data.frame(Table_1=unname(x.fore$pred), Table_2=unname(rn25_29_t$ambtemp))
errorr <- transform(errorr, Result = ifelse(Table_1 - 0.3 < Table_2 & Table_2 < Table_1 + 0.3, 'Normal', 'Outlier'))

Sample data:

   Table_1 Table_2  Result
1 5.778986    5.58  Normal
2 5.768515    6.50 Outlier
3 5.758068    5.83  Normal
4 5.747644    5.54  Normal
5 5.737245    5.80  Normal
6 5.726869    6.03 Outlier

Expected result:

5.768515 -  6.50 = -0.731485
5.726869 -  6.03 = -0.303131

解决方案

Also, maybe (although the output is less pretty)

diff(t(dat[dat$Result == "Outlier", 2:1]))
##                 2         6
## Table_1 -0.731485 -0.303131

这篇关于如果功能在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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