Re: [R] Adding collumn to existing data frame

2010-08-04 Thread Jeff Newmiller
Sometimes we try to make things behave the way we think they ought to and find it surprisingly difficult. Later we discover that our original premise was flawed and we wasted our time trying to force fit our ideas to work. Since all of the i-th elements of the columns of a data table are suppose

Re: [R] Adding collumn to existing data frame

2010-08-03 Thread Ralf B
Data frame is given by the rest of the script and not really an option. Other than that, you are absolutely right. Ralf On Tue, Aug 3, 2010 at 11:34 PM, Dennis Murphy wrote: > Wouldn't a list be a better object type if the variables you want to add > have variable lengths? This way you don't hav

Re: [R] Adding collumn to existing data frame

2010-08-03 Thread Dennis Murphy
Wouldn't a list be a better object type if the variables you want to add have variable lengths? This way you don't have to worry about nuisances such as NA padding. Just a thought... Dennis On Tue, Aug 3, 2010 at 7:54 PM, Ralf B wrote: > Actually it does -- one has to use feed the result back i

Re: [R] Adding collumn to existing data frame

2010-08-03 Thread Ralf B
Actually it does -- one has to use feed the result back into the original variable: add.col <- function(df, vec, namevec){ if (nrow(df) < length(vec) ){ df <- # pads rows if needed rbind(df, matrix(NA, length(vec)-nrow(df), ncol(df), dimnames=list( NULL, names(df)

Re: [R] Adding collumn to existing data frame

2010-08-03 Thread David Winsemius
On Aug 3, 2010, at 10:35 PM, David Winsemius wrote: On Aug 3, 2010, at 8:32 PM, Ralf B wrote: Hi experts, I am trying to write a very flexible method that allows me to add a new column to an existing data frame. This is what I have so far: add.column <- function(df, new.col, name) {

Re: [R] Adding collumn to existing data frame

2010-08-03 Thread David Winsemius
On Aug 3, 2010, at 8:32 PM, Ralf B wrote: Hi experts, I am trying to write a very flexible method that allows me to add a new column to an existing data frame. This is what I have so far: add.column <- function(df, new.col, name) { n.row <- dim(df)[1] length(new.col) <- n.row

Re: [R] Adding collumn to existing data frame

2010-08-03 Thread Henrique Dallazuanna
Try this: new.col <- 1:2 transform(df, myNewColumn2 = new.col[1:nrow(df)]) On Tue, Aug 3, 2010 at 9:32 PM, Ralf B wrote: > Hi experts, > > I am trying to write a very flexible method that allows me to add a > new column to an existing data frame. This is what I have so far: > > add.column <- fu

Re: [R] Adding collumn to existing data frame

2010-08-03 Thread Joshua Wiley
On Tue, Aug 3, 2010 at 5:32 PM, Ralf B wrote: > Hi experts, > > I am trying to write a very flexible method that allows me to add a > new column to an existing data frame. This is what I have so far: The existing way is fairly flexible, just name the new data... > mydf <- data.frame(a = c(1, 2,

[R] Adding collumn to existing data frame

2010-08-03 Thread Ralf B
Hi experts, I am trying to write a very flexible method that allows me to add a new column to an existing data frame. This is what I have so far: add.column <- function(df, new.col, name) { n.row <- dim(df)[1] length(new.col) <- n.row names(new.col) <- name return(