如何从数据帧中的日期中仅提取年份?

时间:2015-10-23 12:11:20

标签: r

这是我的数据......&我只需要使用基本的R(不要使用mysql,php,python,c#或任何其他)来提取数据

**service**        **Date**
disconnected        2013-01-14
disconnected        2013-03-15
disconnected        2012-02-24
disconnected        2012-12-05
disconnected        2012-06-08
disconnected        2011-05-08 
disconnected        2010-10-11 
disconnected        2010-12-02

我需要提取的数据只有一年......从日期开始....&后来我需要将它分配给新的变量或向量.....

以下输出应为....

OUTPUT
**service**        **Date**
disconnected        2013
disconnected        2013
disconnected        2012
disconnected        2012
disconnected        2012
disconnected        2011 
disconnected        2010 
disconnected        2010

2 个答案:

答案 0 :(得分:2)

有很多选择。一种方法是使用substr从“日期”列中获取前4个字符元素(假设我们不会返回> 1000)

 df1$Year <- substr(df1$Date, 1,4)

或者我们将从-后跟一个或多个字符开头的子字符串与字符串末尾匹配,并使用''替换为sub

df1$Year <- sub('-.*$', '', df1$Date)

或者我们可以通过转换为year

来提取POSIXlt
 strptime(df1$Date, '%Y-%m-%d')$year+1900

如果我们被允许使用套餐,library(lubridate)有一个方便的功能,即year

library(lubridate)
year(df1$Date)

数据

df1 <- structure(list(service = c("disconnected", "disconnected", "disconnected", 
"disconnected", "disconnected", "disconnected", "disconnected", 
"disconnected"), Date = c("2013-01-14", "2013-03-15", "2012-02-24", 
"2012-12-05", "2012-06-08", "2011-05-08", "2010-10-11", "2010-12-02"
)), .Names = c("service", "Date"), class = "data.frame",
row.names = c(NA, -8L))

答案 1 :(得分:1)

如果你使date成为日期变量,format可以很容易地推出年份。

D <- data.frame(service = rep("disconnected", 3),
                date = c("2013-01-14", "2013-03-15", "2012-02-24"))

D$year <- format(as.Date(D$date), format = "%Y")

D

       service       date year
1 disconnected 2013-01-14 2013
2 disconnected 2013-03-15 2013
3 disconnected 2012-02-24 2012