Data file 'example.dat' has this format: stream,sampdate,param,quant B,1992-03,Cl,4 B,1992-03,SO4,33 B,1992-03,pH,8.43 B,1992-04,Cl,4 B,1992-04,SO4,32 B,1992-04,pH,8.46 B,1992-05,Cl,4 B,1992-05,SO4,31 B,1992-05,pH,8.43 It's read into R with allchem <- read.table('example.dat', stringsAsFactors=F, header=T, sep=',') and yields this structure: str(allchem) 'data.frame': 2226 obs. of 4 variables: $ stream : chr "B" "B" "B" "B" ... $ sampdate: chr "1992-03" "1992-03" "1992-03" "1992-04" ... $ param : chr "Cl" "SO4" "pH" "Cl" ... $ quant : num 4 33 8.43 4 32 8.46 4 31 8.43 6 ... Because the date field contains year and month but no day, as.Date() does not work. ?as.Date displays the help file for yearmon in package 'zoo.' Reading this lead me to try: allchem$sampdate <- as.yearmon(format(%Y-%m))allchem$sampdate which produces the error, Error: unexpected SPECIAL in "allchem$sampdate <- as.yearmon(format(%Y-%". I do not see the proper syntax to change the sampdate char string to year-month dates. Advice appreciated. Rich
Rich Shepard
2015-Jun-26 19:04 UTC
[R] Formatting YYYY-MM after reading text file [RESOLVED]
On Fri, 26 Jun 2015, Rich Shepard wrote:> allchem$sampdate <- as.yearmon(format(%Y-%m))allchem$sampdateReading the yearmon help page again led me to try allchem$sampdate <- as.yearmon(allchem$sampdate) which produces the following structure: 'data.frame': 2226 obs. of 4 variables: $ stream : chr "B" "B" "B" "B" ... $ sampdate:Class 'yearmon' num [1:2226] 1992 1992 1992 1992 1992 ... $ param : chr "Cl" "SO4" "pH" "Cl" ... $ quant : num 4 33 8.43 4 32 8.46 4 31 8.43 6 ... which appears to do what's needed: allchem stream sampdate param quant 1 B Mar 1992 Cl 4.000 2 B Mar 1992 SO4 33.000 3 B Mar 1992 pH 8.430 4 B Apr 1992 Cl 4.000 5 B Apr 1992 SO4 32.000 6 B Apr 1992 pH 8.460 7 B May 1992 Cl 4.000 8 B May 1992 SO4 31.000 9 B May 1992 pH 8.430 10 B Jun 1992 Cl 6.000 Not having before worked with dates like this I'll soon see what happens as the analyses proceed. Rich
as.yearmon(allchem$sampdate) worked for me. David On 26 June 2015 at 19:44, Rich Shepard <rshepard at appl-ecosys.com> wrote:> Data file 'example.dat' has this format: > > stream,sampdate,param,quant > B,1992-03,Cl,4 > B,1992-03,SO4,33 > B,1992-03,pH,8.43 > B,1992-04,Cl,4 > B,1992-04,SO4,32 > B,1992-04,pH,8.46 > B,1992-05,Cl,4 > B,1992-05,SO4,31 > B,1992-05,pH,8.43 > > It's read into R with > allchem <- read.table('example.dat', stringsAsFactors=F, header=T, sep=',') > > and yields this structure: > > str(allchem) > 'data.frame': 2226 obs. of 4 variables: > $ stream : chr "B" "B" "B" "B" ... > $ sampdate: chr "1992-03" "1992-03" "1992-03" "1992-04" ... > $ param : chr "Cl" "SO4" "pH" "Cl" ... > $ quant : num 4 33 8.43 4 32 8.46 4 31 8.43 6 ... > > Because the date field contains year and month but no day, as.Date() does > not work. ?as.Date displays the help file for yearmon in package 'zoo.' > Reading this lead me to try: > > allchem$sampdate <- as.yearmon(format(%Y-%m))allchem$sampdate > > which produces the error, Error: unexpected SPECIAL in "allchem$sampdate <- > as.yearmon(format(%Y-%". > > I do not see the proper syntax to change the sampdate char string to > year-month dates. Advice appreciated. > > Rich > > ______________________________________________ > 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.
MacQueen, Don
2015-Jun-26 21:24 UTC
[R] Formatting YYYY-MM after reading text file [RESOLVED]
I would have just assigned them all to the first day of the month, using as.Date( paste0(allchem$sampdate,'-01') ) (or maybe the middle of the month represented by the 15th) and then had a variable that was of the Date class in the base R (with which I am familiar, no small consideration). Depending on what needs to be done with them -- plotting with a date axis? -- calculating elapsed time between sampling events? -- I suppose one or the other of 'yearmon' or 'Date' might be more convenient. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 6/26/15, 12:04 PM, "R-help on behalf of Rich Shepard" <r-help-bounces at r-project.org on behalf of rshepard at appl-ecosys.com> wrote:>On Fri, 26 Jun 2015, Rich Shepard wrote: > >> allchem$sampdate <- as.yearmon(format(%Y-%m))allchem$sampdate > > Reading the yearmon help page again led me to try > allchem$sampdate <- as.yearmon(allchem$sampdate) >which produces the following structure: > >'data.frame': 2226 obs. of 4 variables: > $ stream : chr "B" "B" "B" "B" ... > $ sampdate:Class 'yearmon' num [1:2226] 1992 1992 1992 1992 1992 ... > $ param : chr "Cl" "SO4" "pH" "Cl" ... > $ quant : num 4 33 8.43 4 32 8.46 4 31 8.43 6 ... > >which appears to do what's needed: > >allchem > stream sampdate param quant >1 B Mar 1992 Cl 4.000 >2 B Mar 1992 SO4 33.000 >3 B Mar 1992 pH 8.430 >4 B Apr 1992 Cl 4.000 >5 B Apr 1992 SO4 32.000 >6 B Apr 1992 pH 8.460 >7 B May 1992 Cl 4.000 >8 B May 1992 SO4 31.000 >9 B May 1992 pH 8.430 >10 B Jun 1992 Cl 6.000 > > Not having before worked with dates like this I'll soon see what >happens >as the analyses proceed. > >Rich > >______________________________________________ >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.