[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-help
Subject:    Re: [R] Add row to data frame
From:       "Pascal A. Niklaus" <Pascal.Niklaus () unibas ! ch>
Date:       2003-12-08 13:59:32
[Download RAW message or body]

Martin Maechler wrote:

>>>>>>"Pascal" == Pascal A Niklaus <Pascal.Niklaus@unibas.ch>
>>>>>>    on Mon, 08 Dec 2003 11:47:02 +0100 writes:
>>>>>>            
>>>>>>
>
>    Pascal> Hi all, is there an easy way to build up a data
>    Pascal> frame by sequentially adding individual rows? 
>
>yes, pretty easy, but usually not recommended because quite
>inefficient.
>
>rbind() does work with data frames in the cases we know.
>Have a look at help(rbind.data.frame)
>
>
>    Pascal> The data frame consists of numeric and character
>    Pascal> columns. I thought of rbind, but I ended up with
>    Pascal> numeric values for the character columns.
>
>We'd need to see [i.e give a reproducible example!]
>how you "ended up with numeric values for the  character
>columns" -- which I guess were *factor* instead of character ?
>  
>
Yes, there was a factor...  There's one problem left, though... The row 
names are 1, 11, 111 etc, instead of 1,2,3...

 > df <- NULL;
 >
 > df <- rbind(df,data.frame(A=1,B="abc",C=rnorm(1)))
 > df <- rbind(df,data.frame(A=1,B="abc",C=rnorm(1)))
 > df <- rbind(df,data.frame(A=1,B="abc",C=rnorm(1)))
 > df
    A   B          C
1   1 abc  1.3540030
11  1 abc -0.7229597
111 1 abc -0.4922653

Of course, I can do a  attr(df,"row.names") <- 1:3 at the end, but is 
there an easier way? Ideally, I would like to name the row already when 
adding it to the data frame. Is there another way than setting the 
row.names attribute "manually" with the attr command?

Pascal


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic