Hi all, Is there an equivalent to the general * operator in R, where * can represent any character? I have a dataset, one column being date, date ranging between April-September, 97-06. I would like to be able to acquire the data for a specific month, say September, so that I can take average temperatures for the month, etc. I thought I would be able to do something like data.s1 <- subset(data,date=="**-Sep-**") data.s1 but when I do data.s1 I get "0 rows or 0-length row.names". Any idea of a simple way I can perform this task? All dates are in the form xx-xxx-xx, EG 30-Sep-06. Thanks for any assistance. Robin Williams Met Office summer intern - Health Forecasting robin.williams@metoffice.gov.uk [[alternative HTML version deleted]]
Williams, Robin wrote:> Hi all, > Is there an equivalent to the general * operator in R, where * can > represent any character? > I have a dataset, one column being date, date ranging between > April-September, 97-06. I would like to be able to acquire the data for > a specific month, say September, so that I can take average temperatures > for the month, etc. I thought I would be able to do something like > data.s1 <- subset(data,date=="**-Sep-**") > data.s1 > but when I do > data.s1 > I get "0 rows or 0-length row.names". > Any idea of a simple way I can perform this task? > All dates are in the form xx-xxx-xx, EG 30-Sep-06. > Thanks for any assistance.grep() and related functions described on the same man page can match general regular expressions. The pattern you want would be "^..-Sep-..$". Read the man page; they have several options for output format. They work on character vectors; if your data has already been converted to a date format, there are likely other options, e.g. converting the date with format %b (see ?strftime) so only the month is included. Duncan Murdoch
Something like this might do what you want: dates<-paste(c("2008"), c("Jan", "Feb", "Sep", "Sep", "Dec"), 1:3, sep="") temp<-rnorm(length(dates))*30 data1<-data.frame(Dates=dates, Temp=temp) data.s1<-data1[grep("Sep", data1[["Dates"]]),]> data.s1Dates Temp 3 2008Sep3 22.263627 4 2008Sep1 9.854643 Regards JS --- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Williams, Robin Sent: 24 July 2008 11:31 To: r-help at r-project.org Subject: [R] Is there an equivalent * operator? Hi all, Is there an equivalent to the general * operator in R, where * can represent any character? I have a dataset, one column being date, date ranging between April-September, 97-06. I would like to be able to acquire the data for a specific month, say September, so that I can take average temperatures for the month, etc. I thought I would be able to do something like data.s1 <- subset(data,date=="**-Sep-**") data.s1 but when I do data.s1 I get "0 rows or 0-length row.names". Any idea of a simple way I can perform this task? All dates are in the form xx-xxx-xx, EG 30-Sep-06. Thanks for any assistance. Robin Williams Met Office summer intern - Health Forecasting robin.williams at metoffice.gov.uk [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
If you really want to work with character strings, you could do something like data.s1 <- data$date[grep(".*-Sep-.*", date)] (see ?regexp for more on pattern matching) but there are better ways to handle dates. You should convert the character strings into Date objects: see ?Date. And check out the zoo package. -Felix On Thu, Jul 24, 2008 at 8:30 PM, Williams, Robin <robin.williams at metoffice.gov.uk> wrote:> Hi all, > Is there an equivalent to the general * operator in R, where * can > represent any character? > I have a dataset, one column being date, date ranging between > April-September, 97-06. I would like to be able to acquire the data for > a specific month, say September, so that I can take average temperatures > for the month, etc. I thought I would be able to do something like > data.s1 <- subset(data,date=="**-Sep-**") > data.s1 > but when I do > data.s1 > I get "0 rows or 0-length row.names". > Any idea of a simple way I can perform this task? > All dates are in the form xx-xxx-xx, EG 30-Sep-06. > Thanks for any assistance. > > > Robin Williams > Met Office summer intern - Health Forecasting > robin.williams at metoffice.gov.uk > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Felix Andrews / ??? PhD candidate Integrated Catchment Assessment and Management Centre The Fenner School of Environment and Society The Australian National University (Building 48A), ACT 0200 Beijing Bag, Locked Bag 40, Kingston ACT 2604 http://www.neurofractal.org/felix/ 3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8
Hi Robin, to answer your question: I think you are looking for a regular expression, have a look at regex(). In your particular case, where all the strings show the same structure, you can also use substr() to cut out the substring of interest. More generally, you could consider to convert your your string-dates to real date information with strptime(). After doing so, you can easily calculate functions on various date combinations with format() and tapply().> date<-strptime(data$date,format="%d-%B-%y") > tapply(data$temperature,format(date,format="%m"),FUN=mean,na.rm=F) > # or, if you only want the results for september, use which() > mean(data$temperature[which(format(date,format="%m")==9)])Read the help files on the details. :) Cheers, Ren?> -----Urspr?ngliche Nachricht----- > Von: "Williams, Robin" <robin.williams at metoffice.gov.uk> > Gesendet: 24.07.08 14:18:13 > An: <r-help at r-project.org> > Betreff: [R] Is there an equivalent * operator?> Hi all, > Is there an equivalent to the general * operator in R, where * can > represent any character? > I have a dataset, one column being date, date ranging between > April-September, 97-06. I would like to be able to acquire the data for > a specific month, say September, so that I can take average temperatures > for the month, etc. I thought I would be able to do something like > data.s1 <- subset(data,date=="**-Sep-**") > data.s1 > but when I do > data.s1 > I get "0 rows or 0-length row.names". > Any idea of a simple way I can perform this task? > All dates are in the form xx-xxx-xx, EG 30-Sep-06. > Thanks for any assistance. > > > Robin Williams > Met Office summer intern - Health Forecasting > robin.williams at metoffice.gov.uk > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >____________________________________________________________________ Ihre Messenger, Communities und E-Mails jetzt in einem Programm!