4

This is my data...& i need to extract data by using only basic R(dont use mysql, php,python ,c# or any other)

**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

The data i need to extract is only year...from the date....& later again i need to assign it to new variable or vector.....

the following output should be....

OUTPUT
**service**        **Date**
disconnected        2013
disconnected        2013
disconnected        2012
disconnected        2012
disconnected        2012
disconnected        2011 
disconnected        2010 
disconnected        2010
arshia
  • 65
  • 1
  • 1
  • 6

2 Answers2

6

There are many options. One way is using substr to get the first 4 character elements from 'Date' column (assuming that we are not going back to > 1000 )

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

Or we match the substring that begins from - followed by one or more characters to the end of the string, replace with '' using sub.

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

Or we can extract the year by converting to POSIXlt class

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

If we are allowed to use packages, library(lubridate) has a convenient function i.e. year

library(lubridate)
year(df1$Date)

data

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))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • thnq u :) that strptime command worked....i gave that data for example actually i hv large amount of data...lyk abut 309 records so i cant specify so many dates....Date=c("...")...how do i assign the modified data to new variable for ex:Z& dat Z should contain only service column & the modified date thats year. – arshia Oct 23 '15 at 13:12
  • @arshia I used `dput` after reading the file using `read.table` to get the structure as shown in the post. In your case, you need just `df1 <- read.table('yourfile.txt', header=TRUE, stringsAsFactors=FALSE)` if I understand your comment. – akrun Oct 23 '15 at 16:27
  • yeah got it thnx a lot :) – arshia Oct 25 '15 at 10:34
  • Year:num[1:554] 2013 2013 2013.....2012 2012.... "Year" is the value & need to add it to my dataframe "mydata" & mydata consists of 554 records......"service" & "Date" I striped the years from the date & want to add it to existing dataframe... – arshia Oct 27 '15 at 17:11
  • I have 554 recordes & i can't specify in the dates in the following command : date = c("2013-01-14", "2013-03-15", "2012-02-24")) ...How to load 554 records date.....?? if i use the above command – arshia Oct 27 '15 at 17:28
  • @arshia If you have saved the 'date' as a `.csv/.txt` file with 'date' as a single column, just read it using `df1 <- read.csv('yourfile.txt', header=TRUE, stringsAsFactors=FALSE)` In case, it is stored as a text, use `scan` i.e. `scan('yourfile.txt', sep='', what='')` BTW, the `structure(list(...` output is from the `dput` just to make this reproducible. – akrun Oct 28 '15 at 04:16
  • It is saved as .csv but after slicing that year column by using that strptime... i got its values....Year:num[1:554] 2013 2013 2013.....2012 2012...nw i want to bind dat Year values to existing dataframe......cbind i used but that year column does not contain "2013" "2012"....& instead "year" is getting printed in that column. – arshia Oct 28 '15 at 16:27
  • @arshia Can you post this as a new question as I am not able to understand this from the comments. – akrun Oct 28 '15 at 16:34
  • 1
    Thanks. It works – Mohamed Rahouma Sep 21 '20 at 20:11
3

If you make date a Date variable, format can pull out the year quite easily.

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
Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • thnq u :) i gave that data for example actually i hv large amount of data...lyk abut 309 records so i cant specify so many dates....Date=c("...")...how do i assign the modified data to new variable for ex:Z& dat Z should contain only service column & the modified date thats year. – arshia Oct 23 '15 at 13:13
  • I have 554 recordes & i can't specify in the dates in the following command : date = c("2013-01-14", "2013-03-15", "2012-02-24")) ...How to load 554 records date.....?? – arshia Oct 27 '15 at 17:19