我想在数据库中找到年龄,我用这个答案来解决问题Get the difference between dates in terms of weeks, months, quarters, and years?
这段代码我以前找了几年
age <- function(dob, age.day = today(), units = "years", floor = TRUE) {
calc.age = interval(dob, age.day) / duration(num = 1, units = units)
if (floor) return(as.integer(floor(calc.age)))
return(calc.age)
}
但是当我使用for循环执行循环并尝试在数据库客户的新列中保存值时,我无法这样做。
for (i in customer$dateofbirth) {
as.Date(i)
customer$age <- age(i)
}
我得到的结果是年龄栏中的相同值。我哪里错了?
答案 0 :(得分:2)
如果你想使用var PythonShell = require('python-shell');
PythonShell.run('my_script.py', function (err) {
if (err) throw err;
console.log('finished');
});
循环,也许这很有用
for
@RHertel感谢评论for (i in customer$dateofbirth)
customer$age[i] <- age(as.Date(i))
答案 1 :(得分:1)
library(dplyr)
customer <- customer %>%
rowwise() %>%
mutate(age = age(as.Date(dateofbirth)))