[R] Formatting YYYY-MM after reading text file
Rich Shepard
rshepard at appl-ecosys.com
Fri Jun 26 20:44:39 CEST 2015
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
More information about the R-help
mailing list