A simple problem needs help【number of items to replace is not a multiple of replacement length】

Welcome to this community.

To get more helpful answers, please ask questions with a reproducible example. If you don't know how, here's a helpful link:

Now, I generated some birthdays and calculated ages at 2019-02-28. It seems to work correctly without any error. See here:

set.seed(seed = 24971)

simulated_birthdays <- sample(x = seq(from = as.Date(x = '1991/01/01'),
                                      to = as.Date(x = '2000/12/31'),
                                      by = 11))

head(x = simulated_birthdays)
#> [1] "1992-06-12" "1991-03-19" "1992-04-29" "1999-04-03" "1991-06-04"
#> [6] "1997-03-05"

current_date <- as.Date(x = '2019/02/28')

ages_in_days <- difftime(time1 = current_date,
                         time2 = simulated_birthdays,
                         units = 'days')

head(x = ages_in_days)
#> Time differences in days
#> [1]  9757 10208  9801  7271 10131  8030

approximate_ages_in_years <- round(x = as.numeric(x = (ages_in_days / 365.25)),
                                   digits = 2)

head(x = approximate_ages_in_years)
#> [1] 26.71 27.95 26.83 19.91 27.74 21.98

Created on 2019-02-28 by the reprex package (v0.2.1)

Can you please check whether you are in a better luck with dat$age<-round(as.numeric((difftime(as.Date("2017-12-31"), dat$BIRTHDAY, units = "days"))/365.25),digits = 2)?

1 Like