0

I have a dataframe (SCWB) from 2001 with 5500 observations (people). The variable YRBIRTH captures the individuals' year of birth. I would like to create a new variable called AGE --> "AGE = 2001 - YRBIRTH" I can't seem to figure out how to code this variable into R - so far I have:

SCWB$AGE <- lapply(SCWB, transform, YRBIRTH = 2001 - YRBIRTH)

And an error message:

Error: unexpected '=' in "SCWB$YrBrth <- lapply(transform, SCWB$YRBIRTH ="

Any ideas on how to fix this?

Thank you!

1 Answer 1

1

If 'SCWB' is a data.frame, we can directly apply transform on the column 'YRBIRTH' instead of a loop with lapply

SCWB <- transform(SCWB, YRBIRTH = 2001 - YRBIRTH)
1
  • 1
    Thank you this was exactly what I was looking for!! May 24, 2020 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.