[R] Creating a sequence of variables in a data frame

Jason Turner jasont at indigoindustrial.co.nz
Mon Mar 10 08:47:39 CET 2003

On Mon, Mar 10, 2003 at 01:08:43AM -0500, Ryan T. Moore wrote:
> Two questions:
> 1.  I have a data frame named "data1" that includes the variable
> "old".  I want to create a sequence of new variables in the data frame
> called "old.1", "old.2", ... .  I've tried a few "paste" commands, and
> creating a new data frame of the new variables, all to no avail.  Any
> advice? 

I'd just make a dummy column in the loop, then re-write the column
name mid-loop.  e.g.

foo <- data.frame(old = sample(5,100,replace=TRUE)) #generate some data
for(ii in 1:5) {
  foo$zz <- vector(mode="logical",length=length(foo$old))
  names(foo)[ii + 1] <- paste(sep="","old.",ii) #nb - offset 1; yours
                                                #will probably vary

> 2.  If I have a variable in a data frame, is there quick bit of code
> that creates a dummy variable for each level of that variable? 

Depends what you mean by "quick" ... ;).  I'm sure Thomas Lumley could
blast out a one-liner with reshape() that does exactly what you need,
but I'm pretty clunky with those.  I'd use the above, and roll it
through another loop.

for(jj in 1:5) { 
  foo[[jj + 1]] [which(foo$old == jj)] <- TRUE #note - same offset
                                               #from above

Again, I'm sure the gurus have a slick way of doing it.  I'm also sure
some of them wouldn't like to think of the above examples while eating
dinner ;)

Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz

More information about the R-help mailing list