r - Returning a true value if it is a close match among different columns in r
问题描述
The data is as follows :
a <- c('id1','id2','id3','id4','id5')
b <- c(5,10,7,2,3)
d <- c(5.2,150,123,5,7)
e <- c(5.4,0,10,3,5)
df1 <- data.frame(a,b,d,e)
I want to create a new column in this data frame returning TRUE and FALSE. It should be true if all the values are within 5% difference of each other, else false.
For example, for 'id1' the values are 5,5.2,5.4 respectively for b,d and e column. So all these are within 5% of each other hence the new_col should be true. For 'id2' the values are 10,150,0 respectively for b,d and e column.So, they are not with 5% of each other, hence it should be false.
Desired Output
解决方案
This looks at 1.05 times the minimum values is less than the 0.95 times the maximum value for each of the rows. (I assumed that's what you meant by within %5 of each other.)
sapply(1:nrow(df1), function(i) (min(df1[i, 2:4]) * 1.05) >
(0.95 * max(df1[i, 2:4])))
# [1] TRUE FALSE FALSE FALSE FALSE
Slightly different way to do the same.
sapply(1:nrow(df1), function(i) diff(range(df1[i, 2:4]) *
c(1.05, 0.95)) <= 0)
# [1] TRUE FALSE FALSE FALSE FALSE
推荐阅读
- database - Postgres - 将来自 2 个不同表的数据组合到 1 个表中
- json - 将json中的时间戳对象转换为字符串形式的时间戳
- laravel - 如何从 Google Analytics 4 中获取视图 ID?
- c# - 生成 PDF 时的古吉拉特语 托管 AWS Server ASP.NET 不支持
- python - 在 python 3.9 中安装 cvlib 包
- python - Divmod() 逻辑将克转换为公斤、磅、盎司
- r - R:NbClust 包使用具有随机行顺序的相同数据集给出不同的最佳聚类数
- php - PHP登录在提交时重新加载
- vue.js - nuxt auth 在登录前调用用户并给出 401
- python - 错误 - ValueError:权重的数量与 python 3 中的总体不匹配