Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df05053110043bd520ba@mail.gmail.com>
Date: 2005-05-31T17:04:08Z
From: Gabor Grothendieck
Subject: Add Columns and Order for Rbind?
In-Reply-To: <OF81B6A0B5.238B5180-ON86257012.0058EF5A-86257012.005AC726@fd9ns01.okladot.state.ok.us>

On 5/31/05, khobson at fd9ns01.okladot.state.ok.us
<khobson at fd9ns01.okladot.state.ok.us> wrote:
> 
> 
> 
> 
> I am using rbind to add one list with one row to another master list.  The
> problem is that not all columns exist in both lists.
> 
> What methods would you recommend to reorder one list based on the column
> names of another?   If both sets have the same order and columns, I can
> then use rbind.  I can live with columns being added to the master list set
> it makes the task easier using something like union for column name
> matching and order.
> 

Suppose we have this test data:

# test data
irish <- head(iris)
one.row <- data.frame(Sepal.Length = 5, Sepal.Area = 10)

# Then we rbind an NA row to irish and fill it with one.row

irish <- rbind(irish, NA)
irish[nrow(irish),names(one.row)] <- one.row


# If you don't want columns added to the master list do this instead:

irish <- head(iris)  # recreate test data
irish  <- rbind(irish, NA)
both <- intersect(names(irish), names(one.row))
irish[nrow(irish), both] <- one.row[,both]

Note that appending rows one at a time can be slow.