[R] vector as data.frame element?

Peng Yu pengyu.ut at gmail.com
Tue Dec 1 04:36:04 CET 2009


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'))




More information about the R-help mailing list