问题
I've seen several entries here on a similar question yet on different platforms - i.e. not using R.
My question, in brief, is: How do I take only the year info from a string variable that also contains the month and day of birth?
Example:
born_in
1 "1974-12-18"
2 ...
Thank you in advance!
回答1:
Here's a single line:
format(as.Date("1974-12-18"),"%Y")
回答2:
Maybe this will help:
born_in <- "1974-12-18"
born_in_date <- as.Date(born_in)
year(born_in_date)
来源:https://stackoverflow.com/questions/47597010/how-to-extract-year-of-birth-from-a-yyyy-mm-dd-string-variable-in-r