help with subset Dates in R

36 views
Skip to first unread message

Brett Scheffers

unread,
Dec 30, 2012, 1:05:25 PM12/30/12
to tropi...@googlegroups.com
Hello Everyone

Happy Holidays!

I'm trying to select data between two dates 2011-29-06 to 2011-25-08  (y/d/m) and am having trouble.
I'm a novice R user and dates in R just confuse me. Any help would be much appreciated.
 
The object I'm working from is derived from multiple .csv files in a single folder and then arranging them in the following:

alldata<-c()
for (ii in 1: length(f)){
  d<-read.csv(file=f[ii], header=T)
  t1<-sub(pattern=":", replacement=".", x=d$DateTime)
  t1<-as.vector(regexpr(pattern=":", text=t1))
  fmat<-ifelse(t1==-1, "%d/%m/%Y %H:%M", "%d/%m/%Y %H:%M:%S")
  d$tdate=strptime(d$DateTime, format=fmat)
  d$date<-as.Date(format(d$tdate, "%d/%m/%y"), format="%d/%m/%y")
  d$mdata<-rep(test2[ii], nrow(d))
  d$elev<-as.numeric(rep(test3[ii], nrow(d)))
  d$gc<-rep(test4[ii], nrow(d))
  if(nrow(d)==0) print(ii)
  alldata<-rbind(alldata, d) 
}

The alldata object are in the following data format:

str(alldata)
'data.frame': 578229 obs. of 8 variables: $ DateTime: Factor w/ 11390 levels "1/8/2011 0:14",..: 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 ... $ Temp..C.: num 20.1 20.1 20.6 20.6 20.6 ... $ Hum.... : num 97.1 96.7 97.1 96.7 97.1 ... $ tdate : POSIXlt, format: "2011-07-05 11:59:00" "2011-07-05 12:14:00" "2011-07-05 12:29:00" ... $ date : Date, format: "2011-07-05" "2011-07-05" "2011-07-05" ... $ mdata : chr "1100TR1-Canopy" "1100TR1-Canopy" "1100TR1-Canopy" "1100TR1-Canopy" ... $ elev : num 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 ... $ gc : chr "Canopy" "Canopy" "Canopy" "Canopy" ...

The data I'm interested in are the alldata$date data which as you can see are already in a date format by use of the as.Date().

I would like to subset the data from alldata$gc (which is either Canopy or Ground), alldata$elev (which all data are an elevation between 900 and 2100), and then the alldata$date (in my case dates from 2011-29-06 to 2011-25-08). 

My script is:
subdata <- subset(alldata, subset=(alldata$gc == 'Canopy') & alldata$elev == 1300 & alldata$date >= "2011-29-06" & alldata$date <= "2011-25-08" , select=c(Temp..C., elev, gc, date) )

this does not work.
The following error shows:
"
Error in charToDate(x) : character string is not in a standard unambiguous format"

Again, any help that you can offer would be greatly appreciated.

Cheers
Brett Scheffers

Phillips, Ben

unread,
Dec 30, 2012, 4:13:12 PM12/30/12
to <tropical-r@googlegroups.com>
Brett. 

In you final subset you need to change your boundary dates into date objects. 

alldata$date >= as.Date("2011-29-06", format = "%Y-%d-%m")

B

Excuse the brevity: sent from my phone.
--
An R group for questions, tips and tricks relevant to spatial ecology and climate change.
All R questions welcome.
---
You received this message because you are subscribed to the Google Groups "Tropical R" group.
To post to this group, send an email to tropi...@googlegroups.com.
To unsubscribe from this group, send email to tropical-r+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Stewart Macdonald

unread,
Dec 30, 2012, 4:19:51 PM12/30/12
to tropi...@googlegroups.com
Out of interest, does it work if you use "2011-06-29" instead of "2011-29-06"? It looks like R is trying to convert it to a date object automatically, but is getting confused. But as Ben said, it's better to explicitly convert it rather than rely on automatic and potentially ambiguous conversion.


Stewart
Reply all
Reply to author
Forward
0 new messages