首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R:在特定的行和列上循环自定义函数

R:在特定的行和列上循环自定义函数
EN

Stack Overflow用户
提问于 2019-01-21 17:57:58
回答 1查看 25关注 0票数 0

我想循环遍历选定的列和它们的所有行,并应用一个If Else嵌套函数来替换所有元素。

代码语言:javascript
复制
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,结果与我预期的不完全一样。是否有其他方法可以生成此输出?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-21 18:07:10

代码语言:javascript
复制
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)))))})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54287398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档