How to extract year of birth from a “YYYY-MM-DD” string variable in R? [duplicate]

假装没事ソ 提交于 2019-12-25 19:03:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!