r - R:在特定的行和列上循环自定义函数
问题描述
我想遍历选择的列和所有行,并应用 If Else 嵌套函数以替换所有元素。
Table[c("Pb","Pc","Pd","Pe","Pf")] <- lapply(Table[c("Pb","Pc","Pd","Pe","Pf")], function(x) {
if (x <0.50) {round_any(x,0.05)}
else if (x <1.00) {round_any(x,0.10)}
else if (x <2.00) {round_any(x,0.25)}
else if (x <5.00) {round_any(x,0.50)}
else {round_any(x,1)}
})
代码运行,但我收到以下警告:
1: In if (x < 0.5) { ... : the condition has length > 1 and only the first element will be used
结果并不完全符合我的预期。还有另一种方法可以产生这个输出吗?
解决方案
Table[c("Pb","Pc","Pd","Pe","Pf")] <- lapply(Table[c("Pb","Pc","Pd","Pe","Pf")], function(x) {
ifelse (x <0.50, round_any(x,0.05),
ifelse (x <1.00, round_any(x,0.10),
ifelse (x <2.00, round_any(x,0.25),
ifelse (x <5.00, round_any(x,0.50),
round_any(x,1)))))})
推荐阅读
- c# - 如何修复 guid 在 vsto Outlook 自定义加载项中始终显示 GUID:{00000000-0000-0000-0000-000000000000}?
- java - 如何从 RestTemplate 获取 JSON 值
- lua - lua中有没有办法做到这一点 Parent.Parent.{{VAR}}.child
- node.js - 如何为在 docker 容器内运行的快速服务器启用 xvfb?
- angular - 在 Angular 组件中添加 element.scrollIntoView() 的位置?
- node.js - 处理节点中列数不等的 csv 文件
- python - statsmodels OLS 的 ValueError
- python - 正则表达式匹配某个字符后的单词
- java - Keycloak自定义用户存储不显示属性
- html - 引导表插件导出选项