[R] Extract year from date

Steve Archambault archstevej at gmail.com
Sun Mar 8 07:50:23 CET 2015


Hi all,

I am trying in vain to create a new object "Year" in my data frame from
existing Date data. I have tried many different approaches, but can't seem
to get it to work. Here is an example of some code I tried.

date1<- as.Date(wells$Date,"%m/%d/%Y")
wells$year<-as.numeric(format(date1, "%Y"))

I am starting with data that looks like this.

        ID  Date DepthtoWater_bgs test test2
1  BC-0004 41163           260.60    3     1
2  BC-0004 41255           261.65    4     2
3  BC-0003 41345           166.58    5     3
4  BC-0002 41351           317.85    6     4
5  BC-0004 41355           262.15    7     5
6  BC-0003 41438           167.55    8     6
7  BC-0004 41438           265.45    9     7
8  BC-0002 41443           317.25   10     8
9  BC-0002 41521           321.25   11     9
10 BC-0003 41522           168.65   12    10
11 BC-0004 41522           266.15   13    11
12 BC-0003 41627           168.95   14    12
13 BC-0004 41627           265.25   15    13
14 BC-0002 41634           312.31   16    14
15 BC-0003 41703           169.25   17    15
16 BC-0004 41703           265.05   18    16
17 BC-0002 41710           313.01   19    17
18 BC-0003 41795           168.85   20    18
19 BC-0004 41795           266.95   21    19
20 BC-0002 41801           330.41   22    20
21 BC-0003 41905           169.75   23    21
22 BC-0004 41905           267.75   24    22
23 BC-0002 41906           321.01   25    23

Any help would be greatly appreciated!

-Steve
Sent from my iPhone



More information about the R-help mailing list