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
Make the question reproducible (I used dput after getting the data into a dataframe). You need to provide a date of origin for the as.Date function. Try lubridate package. library(lubridate) wells <- structure(list(ID = structure(c(3L, 3L, 2L, 1L, 3L, 2L, 3L, 1L, 1L, 2L, 3L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L), .Label = c("BC-0002", "BC-0003", "BC-0004"), class = "factor"), Date = c(41163L, 41255L, 41345L, 41351L, 41355L, 41438L, 41438L, 41443L, 41521L, 41522L, 41522L, 41627L, 41627L, 41634L, 41703L, 41703L, 41710L, 41795L, 41795L, 41801L, 41905L, 41905L, 41906L), DepthtoWater_bgs = c(260.6, 261.65, 166.58, 317.85, 262.15, 167.55, 265.45, 317.25, 321.25, 168.65, 266.15, 168.95, 265.25, 312.31, 169.25, 265.05, 313.01, 168.85, 266.95, 330.41, 169.75, 267.75, 321.01), test = 3:25, test2 = 1:23), .Names = c("ID", "Date", "DepthtoWater_bgs", "test", "test2"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23")) wells$year <- year(as.Date(wells$Date, origin = '1900-1-1')) head(wells$year) Mark R. Mark Sharp, Ph.D. Director of Primate Records Database Southwest National Primate Research Center Texas Biomedical Research Institute P.O. Box 760549 San Antonio, TX 78245-0549 Telephone: (210)258-9476 e-mail: msharp at TxBiomed.org> On Mar 8, 2015, at 12:50 AM, Steve Archambault <archstevej at gmail.com> wrote: > > 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 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
You need to solve step 1 (converting to Date class) before you can solve step 2 (getting the year as a number). In the data you are starting with, the first value in the Date column is 41163. You don't say whether that column is numeric or character, so let's try it both ways:> as.Date('41163','%m/%d/%Y')[1] NA> as.Date(41163,'%m/%d/%Y')Error in charToDate(x) : character string is not in a standard unambiguous format Whatever it is, it is not formatted "%m/%d/%Y", so step 1 fails. On the other hand, if you start with something that is in fact formatted as a date:> tmp <- as.Date('3/2/2015','%m/%d/%Y') > format(tmp,'%Y')[1] "2015"> as.numeric(format(tmp,'%Y'))[1] 2015 you get the year as a number, using the method you tried. So, what do 41163 and all the rest of the numbers in your Date column actually represent??? Mark Sharp is probably correct in expecting that they represent the number of days since some base date, and an easy way to see that would be to try the core of step 1:> as.Date(41163)Error in as.Date.numeric(41163) : 'origin' must be supplied It would be up to you, of course, to find out what the origin is. If 1/1/1900 is correct, then> as.numeric( format( as.Date( 41163, origin='1900-1-1'), '%Y'))[1] 2012 (I personally prefer to use base R as much as possible) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/7/15, 11:50 PM, "Steve Archambault" <archstevej at gmail.com> wrote:>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 > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.