On 06/18/2012 01:49 PM, spf385 wrote:
Greetings,

I'm still very new to R; however I have been tasked with converting an S4
object into a data.frame. We are just trying to learn the basics of S4.
Could anyone offer a 'simple' example of coercing an S4 object (such as
might be stored in a package attribute) into a data.frame? Ultimately, we
would like to do something along the lines of

mydata<- as.data.frame(original S4 object)

see ?setAs for more extensive discussion.

Not sure what 'stored in a package attribute might mean'. Here's a class

 setClass("A", representation(x="numeric", y="numeric"))

and how to coerce from "A" to a "data.frame"

 setAs("A", "data.frame", function(from) data.frame(x=from@x, y=from@y))

and then in action

> a = new("A", x=1:5, y=5:1)
> as(a, "data.frame")
  x y
1 1 5
2 2 4
3 3 3
4 4 2
5 5 1

To use as.data.frame(a), you'd have to implement the 'as.data.frame.A' S3 method, e.g.,

 as.data.frame.A = function(x) as(x, "data.frame")

Also, probably you'd want a validity method (see ?setValidity) on "A", to ensure that x, y were equal length.

Martin


Thanks in advance!

--
View this message in context: 
http://r.789695.n4.nabble.com/S4-into-a-data-frame-tp4633746.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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.


--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

______________________________________________
R-help@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.

Reply via email to