3

So I see for a lot of objects in R there are 2 ways to create an object. For example, for a zoo object there is zoo(....) and as.zoo(....). It looks like the former is an object instantion and the latter a casting construct.

In common languages like C,Java,C# you usually use the casting operator for casting between objects within the same line of inheritance, i.e. cast child to parent classes. You would create a new object if you have a compositional structure (many objects inside 1 object) instead of inheritance.

In R it seems I can pass a dataframe to either zoo(....) and as.zoo(....) and get a zoo object. So when should I use the create object v. using as.xxx?

Denis
  • 11,796
  • 16
  • 88
  • 150
  • 4
    Related [Difference between as.data.frame(x) and data.frame(x)](https://stackoverflow.com/questions/21574250/difference-between-as-data-framex-and-data-framex) – markus Feb 21 '19 at 18:36
  • Each package can define functions however they like. Is this specifically about `zoo`? If not, it's pretty much impossible to answer. – MrFlick Feb 21 '19 at 18:36
  • Is there a general guideline when to use "as.xxx" or when to create an object? – Denis Feb 21 '19 at 18:38
  • 3
    No. You just need to read the documentation for each of the different functions for each package to see which is appropriate. Maybe you can say generally you use `as()` when converting an object from one class to another and the non-as-version when creating an object "from scratch." They sometimes expect different input values. But you should read the docs for any function you plan to use. – MrFlick Feb 21 '19 at 18:41
  • RTFM is not my forte. Isn't there a convention I can follow? – Denis Feb 22 '19 at 22:34

0 Answers0