[R] Converting a pdataframe into dataframe

Peter Ehlers ehlers at ucalgary.ca
Fri Jun 1 18:37:35 CEST 2012


On 2012-06-01 07:09, Apoorva Gupta wrote:
>> a<- data.frame(name=c(rep("a",5), rep("b",5)), year=c(1989:1993, 1989:1993), var=c(1:10))
>> str(a)
>> b<- pdata.frame(a, index=c("name","year"))
>> str(b)
> Now, I want to convert b into a data frame and have a structure
> similar to a. How do I do that?

I assume that pdata.frame() is the function in package plm.
(You should say so.)

The solution depends a bit on just how similar your new data
frame should be to a.

If you don't mind the attributes attached to each variable in b,
then do just

   new.b <- as.data.frame(b)

which will invoke the as.data.frame.pdata.frame() function.

If you want to strip the attributes, then this should do:

   new.b <- lapply(b, function(x){attr(x, "index") <- NULL; x})

In either case, 'year' will still be a factor and you may
still wish to 'unfactor' it; see ?factor.

Peter Ehlers
--



More information about the R-help mailing list