从日期中提取年份 [英] Extract year from date

查看:133
本文介绍了从日期中提取年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从变量中删除前几个元素,尤其是在此变量具有特殊字符的情况下。例如,我有以下列:

How can I remove the first elements from a variable, especially if this variable has a special characters. For example, I have the following column:

Date
01/01/2009
01/01/2010
01/01/2011
01/01/2012

I需要有如下新列:

Date
2009
2010
2011
2012


推荐答案

如果所有日期都是同样的宽度,您可以将日期放入向量中并使用子字符串

if all your dates are the same width, you can put the dates in a vector and use substring

Date
a <- c("01/01/2009", "01/01/2010" , "01/01/2011")
substring(a,7,10) #This takes string and only keeps the characters beginning in position 7 to position 10

输出

[1] "2009" "2010" "2011"

这篇关于从日期中提取年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆