[R] vector as data.frame element?

Remko Duursma remkoduursma at gmail.com
Tue Dec 1 04:47:01 CET 2009


Maybe you can just use a list?

mylis <- list("x", c("y","z"))

element 1 is then mylis[[1]], which is x, and so forth.

But it looks like you really need to 'map' some numbers to your list.
You could use separate vector that keeps track of the index numbers:

index <- c(0,1)

and then map like this:

> mylis[[which(index == 0)]]
[1] "x"
> mylis[[which(index == 1)]]
[1] "y" "z"

Is that what you want?


Remko


-------------------------------------------------
Remko Duursma
Post-Doctoral Fellow

Centre for Plants and the Environment
University of Western Sydney
Hawkesbury Campus
Richmond NSW 2753

Dept of Biological Science
Macquarie University
North Ryde NSW 2109
Australia

Mobile: +61 (0)422 096908
www.remkoduursma.com



On Tue, Dec 1, 2009 at 2:36 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
> It seems that an vector or other non elemental data type can not be
> assigned to an element in the data.frame. I'm wondering what is the
> walk around.
>
>> li=data.frame(a=c(0,1), b=c('x','y'))
>> li$b[[1]]= 'x'
>> li$b[[2]]<- c('y','z')
> Error in li$b[[2]] <- c("y", "z") :
>  more elements supplied than there are to replace
> Execution halted
>
> In the following example, I want the number 1 maps to 'x', but I want
> the number 2 maps to 'y' and 'z'.
>
> I could use the following code. But there is a redundancy in the
> data.frame (the number 1 appears twice). I'm wondering what is the
> best solution to this problem.
>
> li=data.frame(a=c(0,1,1), b=c('x','y','z'))
>
> ______________________________________________
> 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