【发布时间】:2018-09-02 23:51:01
【问题描述】:
我正在尝试编写一个函数来查找一个变量中的零并将这些零替换为除法的结果,包括另一个变量的值。
我的数据:
id apr_days apr_hours
A 1 7
B 2 14
C 0 8
D 0 15
E 0 0
我试图记录一个人使用某项策略的时间量。在 apr_hours > 0 的所有情况下,apr_days 中都需要有一个条目来指示相应的天数(apr_hours /7 的值,向下舍入以仅显示偶数)。
预期输出:
id apr_days apr_hours
A 1 7
B 2 14
C 1 8
D 2 15
E 0 0
我想告诉 R 的是:对于所有具有 apr_hours >0 & apr_days ==0 的观察值,取 apr_hours 的相应值,除以 7;向下舍入到偶数计数值并将apr_days 中的0 替换为此值。
我认为函数的结构应该是这样的:
if apr_hours == 0 {
if true: do nothing
} else {
check whether apr >0
if true: do nothing
} else {
calculate apr_hours/7,
round down to even counts,
and replace the 0 of apr_days with this result
}
我完全迷失了,不知道如何准确地编写代码。任何帮助将不胜感激。
【问题讨论】: