我很想知道儿童的数字时代(例如11.85岁)。我有以下示例数据框:
df.1 <- structure(list(dob = structure(list(sec = c(0, 0, 0, NA, 0, 0 ), min = c(0L, 0L, 0L, NA, 0L, 0L), hour = c(0L, 0L, 0L, NA,
0L, 0L), mday = c(18L, 24L, 25L, NA, 31L, 15L), mon = c(11L,
5L, 11L, NA, 11L, 11L), year = c(100L, 101L, 102L, NA, 99L, 101L
), wday = c(1L, 0L, 3L, NA, 5L, 6L), yday = c(352L, 174L, 358L,
NA, 364L, 348L), isdst = c(0L, 1L, 0L, -1L, 0L, 0L), zone = c("GMT",
"BST", "GMT", "", "GMT", "GMT"), gmtoff = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst",
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), recent.date = structure(list(
sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L
), hour = c(0L, 0L, 0L, 0L, 0L, 0L), mday = c(3L, 2L, 4L,
3L, 1L, 2L), mon = c(5L, 5L, 5L, 5L, 5L, 5L), year = c(114L,
114L, 114L, 114L, 114L, 114L), wday = c(2L, 1L, 3L, 2L, 0L,
1L), yday = c(153L, 152L, 154L, 153L, 151L, 152L), isdst = c(1L,
1L, 1L, 1L, 1L, 1L), zone = c("BST", "BST", "BST", "BST",
"BST", "BST"), gmtoff = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst",
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), decimal.recent.date = c(2014.41906392694,
2014.41632420091, 2014.42180365297, 2014.41906392694, 2014.41358447489,
2014.41632420091)), .Names = c("dob", "recent.date", "decimal.recent.date"
), row.names = c(NA, -6L), class = "data.frame")
然后我使用以下代码将文本转换为日期值:
df.1$dob <- strptime(df.1$dob, "%d/%m/%Y")
df.1$recent.date <- strptime(df.1$recent.date, "%d/%m/%Y")
根据下面列出的评论,我试图在lubridate中使用decimal_date来执行此操作。
到目前为止,我有:
df.1$decimal.dob <- decimal_date(df.1$dob)
df.1$decimal.recent.date <- decimal_date(df.1$recent.date)
由于我的数据框中有一些misisng值,因此出现以下错误: 十进制错误[非零]&lt; - 十进制[非零] /as.numeric(difftime(ceiling_date(date [nonzero],: 订阅作业中不允许使用NA
我试图添加na.rm = T但是很难让它工作。
最后,我希望使用:
df.1$age <- (df.1$recent.date) - (df.1$dob)
...但很明显,直到我得到我的十进制DOB正确,这将无效。
如果有人对如何纠正这一点有任何建议,我将非常感激。
非常感谢提前。