An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110428/7c825274/attachment.pl>
Still confused about classes
5 messages · Joshua Wiley, Brian Ripley, Kenn Konstabel +1 more
Hi Russ,
One tool that might help could be ?methods and ?showMethods
For example:
## for S3
methods(class = "Date")
## for S4
showMethods(classes = "Date")
regarding getting the actual year, I would use (though there may be
better ways):
format.Date(as.Date("2010-01-01"), format = "%Y")
HTH,
Josh
On Thu, Apr 28, 2011 at 11:05 PM, Russ Abbott <russ.abbott at gmail.com> wrote:
Hi, I'm still confused about how to find out what methods are defined for a given class. ?For example, I know that
today <- Sys.Date()
will produce an object of type Date. But I'm not sure what I can do with Date objects or how I can find out.
?Date
refers me to the Date documentation page. But it doesn't tell me how, for example, to extract the current year from a date object. I tried
year(today)Error: could not find function "year"
Is there some other function that does the job? I want a function f such that ? ?> f(today) ? ?will return 2011. Perhaps there is no such function. ?But in general I don't have any confidence that I would know how to find it if it existed or that I would know how to assure myself that there was no such function. Thanks. *-- Russ * ? ? ? ?[[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.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
See ?months. methods(class = "Date") would have got you there. (Date is not an S4 class, so people should not be setting S4 methods on it without very good reason, and there are none in R itself.)
On Thu, 28 Apr 2011, Joshua Wiley wrote:
Hi Russ,
One tool that might help could be ?methods and ?showMethods
For example:
## for S3
methods(class = "Date")
## for S4
showMethods(classes = "Date")
regarding getting the actual year, I would use (though there may be
better ways):
format.Date(as.Date("2010-01-01"), format = "%Y")
Call format(), not its methods, or use strftime() directly.
HTH, Josh On Thu, Apr 28, 2011 at 11:05 PM, Russ Abbott <russ.abbott at gmail.com> wrote:
Hi, I'm still confused about how to find out what methods are defined for a given class. ?For example, I know that
today <- Sys.Date()
will produce an object of type Date. But I'm not sure what I can do with Date objects or how I can find out.
?Date
refers me to the Date documentation page. But it doesn't tell me how, for example, to extract the current year from a date object. I tried
year(today)Error: could not find function "year"
Is there some other function that does the job? I want a function f such that ? ?> f(today) ? ?will return 2011. Perhaps there is no such function. ?But in general I don't have any confidence that I would know how to find it if it existed or that I would know how to assure myself that there was no such function. Thanks. *-- Russ * ? ? ? ?[[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.
-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
______________________________________________ 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.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
The function for getting the year from date is there in package
lubridate (as well as many other convenient functions to work with
dates).
More generally, finding "all" methods for a given class may be a
little tricky. If "all" means everything you have installed and
currently attached to your search path then methods(class="Date") will
do it (for S3 classes). (but "The functions listed are those which
_are named like methods_ and may not actually be methods (known
exceptions are discarded in the code). ") The result depends on which
packages you have loaded: in my currently open R session,
methods("Date") lists 36 "possible methods" but after library(zoo) I
get two more ( "as.yearmon.Date" and "as.yearqtr.Date").
Regards,
Kenn
On Fri, Apr 29, 2011 at 9:05 AM, Russ Abbott <russ.abbott at gmail.com> wrote:
Hi, I'm still confused about how to find out what methods are defined for a given class. ?For example, I know that
today <- Sys.Date()
will produce an object of type Date. But I'm not sure what I can do with Date objects or how I can find out.
?Date
refers me to the Date documentation page. But it doesn't tell me how, for example, to extract the current year from a date object. I tried
year(today)Error: could not find function "year"
Is there some other function that does the job? I want a function f such that ? ?> f(today) ? ?will return 2011. Perhaps there is no such function. ?But in general I don't have any confidence that I would know how to find it if it existed or that I would know how to assure myself that there was no such function. Thanks. *-- Russ * ? ? ? ?[[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110429/d94dc949/attachment.pl>