[R] Data.frames can not hold objects...What can be done in the following scenario?

Duncan Murdoch murdoch.duncan at gmail.com
Sun Jun 10 13:02:42 CEST 2012


On 12-06-10 6:41 AM, Onur Uncu wrote:
> R-Help community,
>
> I understand that data.frames can hold elements of type double, string
> etc but NOT objects (such as a matrix etc).

That is incorrect.  Dataframes can hold list vectors.  For example:

A <- data.frame(x = 1:3)
A$y <- list(matrix(1, 2,2), matrix(2, 3,3), matrix(3,4,4))

A[1,2] will now extract the 2x2 matrix, A[2,2] will extract the 3x3, etc.

Duncan Murdoch

This is not convenient for
> me in the following situation. I have a function that takes 2 inputs
> and returns a vector:
>
> testfun<- function (x,y) seq(x,y,1)
>
> I have a data.frame defined as follows:
>
> testframe<-data.frame(xvalues=c(2,3),yvalues=c(4,5))
>
> I would like to apply testfun to every row of testframe and then
> create a new column in the data.frame which holds the returned vectors
> as objects. Why do I want this? Because the returned vectors are an
> intermediate step towards further calculations. It would be great to
> keep adding new columns to the data.frame with the intermediate
> objects. But this is not possible since data.frames can not hold
> objects as elements. What do you suggest as an elegant solution in
> this scenario? Thank you for any help!
>
>
>
>
>
>
>
>
> I would love to hear if forum
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list