我想循环遍历选定的列和它们的所有行,并应用一个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,结果与我预期的不完全一样。是否有其他方法可以生成此输出?
发布于 2019-01-21 18:07:10
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)))))})https://stackoverflow.com/questions/54287398
复制相似问题