0

I have a dataset with no age-variable. But I have a variable with a year of birth which I want to use to calculate age. But there is a problem that the year of birth is coded as categorical variable. So, I have codes as 1, 2, 3, etc. And years are given as labels to those codes. Is there any simple way to use these labels as numbers to calculate age in R? Thanks in advance, everyone!

upd. It is impossible to use this variable as.numeric since this way it makes calculations with codes (1, 2, 3, etc.), which is wrong calculation.

Phil
  • 7,287
  • 3
  • 36
  • 66
Alina
  • 1
  • 1
  • Please share a reproducible example including packages you are using and the class of the variable. `dput(your_data$year_of_birth[1:10])` would be a perfect way to give us the first 10 values. We really need to know if this is a `factor` or something else. – Gregor Thomas May 03 '23 at 14:53
  • Hi, Gregor! Thanks! I see this:> dput(paneld$Q02[1:10]) structure(c(21, 22, 47, 19, 19, 30, 15, 22, 38, 22), format.spss = "F8.0", labels = c(`1937` = 1, `1941` = 2, `1942` = 3, `1943` = 4, `1944` = 5, `1946` = 6, `1947` = 7, `1948` = 8, `1949` = 9, `1950` = 10, ), class = c("haven_labelled", "vctrs_vctr", "double")) – Alina May 03 '23 at 14:57
  • @Alina The ideal way to add more infos is to use "Edit" below your question. – Andre Wildberg May 03 '23 at 15:39
  • You could try `as.numeric(as.character( your_factor) )` – M Aurélio May 03 '23 at 16:48

0 Answers0