coerce class data type in R with as

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 =

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: 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?

Answers is an S3 method that you can check for foo using :


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


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 :


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:

from="track", to="numeric"

Need Your Help

how to access div with period in its class in jquery


I'm trying to access a div who have a period in its class i.e, but the length returned is 0

how do you change directory on the remote server using jsftp in nodejs


Can anyone tell me how to change the directory on the remote server using jsftp in node js?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.