3

I understand that in R you have some base data types (vector, matrix, list, data.frame) and then in the R packages you have some advanced types called S3-class or S4-class (ppp,owin, spatialPointsDataFrame and many others. Some of the functions in R packages only work with arguments of special type.

I need explanation about converting between different classes and data types in R:

Sometimes I can use a code like:

m = c(1, 2, 3, 4)
df = as.data.frame(m)

But in other cases I must use a code like:

shp = readShapeSpatial("polygons.shp")
win = as(shp,"owin")

How do I know which syntax of the as to use for which object? Or is the syntax: as.foo(originalObject) always equivalent to as(originalObject, "foo") (here foo stands for the class that I want to convert my object to so that I can use in a function that requires its argument to be a foo class)

Let's say I use a package in R with a class foo. And I have a variable v that belongs to class bar (in other words, class(v) is bar). How do I know if the function as(v,"foo") will work?

jirikadlec2
  • 1,256
  • 1
  • 23
  • 36
  • Start here to get a better grasp on classes: http://stackoverflow.com/questions/6583265/what-does-s3-methods-mean-in-r – Thomas Feb 27 '14 at 18:56
  • 2
    You'll know if `as(v,"bar")` will work if you see an entry reading `from="foo", to="bar"` in the results printed by `showMethods("coerce")`. Or check whether `getMethod(coerce, signature=c(from="bar", to="foo"))` returns a function. Or just run `as(v, "bar")` to see if it works ;). – Josh O'Brien Feb 27 '14 at 19:25
  • (Got a few of those `foo`'s and `bar`'s backwards, but you get the point.) – Josh O'Brien Feb 27 '14 at 19:30

1 Answers1

4

as.data.frame is an S3 method that you can check for foo using :

getS3method('as.data.frame','foo')

But I think you are looking for ( as it is commented)

showMethods(coerce)

This will give you a list of predefined coerce funsctions.

To define you coerce function , one option (there are many options like setIS , coerce<- and implicit coercion through inheritance) is to use setAs. Here an example:

track <- setClass("track",
                  slots = c(x="numeric", y="numeric"))
setAs("track", "numeric", function(from) from@y)
t1 <- new("track", x=1:20, y=(1:20)^2)
as(t1, "numeric")

Now if I check using :

showMethods(coerce)

You get an entry with :

from="track", to="numeric"

For better explanation you should read help("as") but the subject is not very simple.

EDIT To show only the entries with track you can do this for example:

cat(grep('track',showMethods(coerce,printTo=FALSE),value=TRUE))
from="track", to="numeric"
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • `showMethods(coerce)` is almost what I'm looking for. The only disadvantage is that the list is very long. Is there a way to show only the entries from `showMethods(coerce)` that contain `to="bar"`? – jirikadlec2 Feb 27 '14 at 19:34
  • @jirikadlec2 you can see my edit. basically I filter the showMethods output. – agstudy Feb 27 '14 at 19:38