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
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
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
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)
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) {
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
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
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,
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(
9 matches
Mail list logo