inplace assignment

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

inplace assignment

David Hugh-Jones-3
I get tired of writing, e.g.


data.frame[some.condition & another.condition, big.list.of.columns] <-
paste(data.frame[some.condition & another.condition,
big.list.of.columns], "foobar")


I would a function like:

inplace(paste(data.frame[some.condition & another.condition,
big.list.of.columns], "foobar"))

which would take the first argument of the inner function and assign
the function's result to it.

Has anyone done something like this? Are there simple alternative
solutions that I'm missing?

Cheers
David

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: inplace assignment

Peter Dalgaard
"David Hugh-Jones" <[hidden email]> writes:

> I get tired of writing, e.g.
>
>
> data.frame[some.condition & another.condition, big.list.of.columns] <-
> paste(data.frame[some.condition & another.condition,
> big.list.of.columns], "foobar")
>
>
> I would a function like:
>
> inplace(paste(data.frame[some.condition & another.condition,
> big.list.of.columns], "foobar"))
>
> which would take the first argument of the inner function and assign
> the function's result to it.
>
> Has anyone done something like this? Are there simple alternative
> solutions that I'm missing?

Well, I'd consider

cc <- some.condition & another.condition
l <- big.list.of.columns
mydf[cc,l] <- paste(mydf[cc,l], "foobar)

--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: inplace assignment

Adaikalavan Ramasamy
In reply to this post by David Hugh-Jones-3
I do not fully understand your question but how about :

 inplace <- function( df, cond1, cond2, cols, suffix ){
 
  w  <- which( cond1 & cond2 )
  df <- df[ w, cols ]
  paste(df, suffix)
  return(df)
 }


BTW, did you mean "colnames(df) <- paste(colnames(df), suffix)" instead
of "paste(df, suffix)" ?

Regards, Adai



On Fri, 2006-06-16 at 10:23 +0100, David Hugh-Jones wrote:

> I get tired of writing, e.g.
>
>
> data.frame[some.condition & another.condition, big.list.of.columns] <-
> paste(data.frame[some.condition & another.condition,
> big.list.of.columns], "foobar")
>
>
> I would a function like:
>
> inplace(paste(data.frame[some.condition & another.condition,
> big.list.of.columns], "foobar"))
>
> which would take the first argument of the inner function and assign
> the function's result to it.
>
> Has anyone done something like this? Are there simple alternative
> solutions that I'm missing?
>
> Cheers
> David
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: inplace assignment

David Hugh-Jones-3
It's more a general point about having to write things out twice when
you do assignments. I could also have written:

data.frame[some.condition & another.condition, big.list.of.columns] <-
data.frame[some.condition & another.condition,  big.list.of.columns] * 2 + 55

or anything else. Equally, there could be any method of subsetting, or
any expression that can be an assignment target, on the left hand
side:

data.frame[[some.complex.expression.for.columnames]]
<-data.frame[[some.complex.expression.for.columnames]] * 333 + foo *
56

rownames(matrix)[45:53] <- paste(rownames(matrix)[45:53], "blah")


David

On 16/06/06, Adaikalavan Ramasamy <[hidden email]> wrote:

> I do not fully understand your question but how about :
>
>  inplace <- function( df, cond1, cond2, cols, suffix ){
>
>   w  <- which( cond1 & cond2 )
>   df <- df[ w, cols ]
>   paste(df, suffix)
>   return(df)
>  }
>
>
> BTW, did you mean "colnames(df) <- paste(colnames(df), suffix)" instead
> of "paste(df, suffix)" ?
>
> Regards, Adai
>
>
>
> On Fri, 2006-06-16 at 10:23 +0100, David Hugh-Jones wrote:
> > I get tired of writing, e.g.
> >
> >
> > data.frame[some.condition & another.condition, big.list.of.columns] <-
> > paste(data.frame[some.condition & another.condition,
> > big.list.of.columns], "foobar")
> >
> >
> > I would a function like:
> >
> > inplace(paste(data.frame[some.condition & another.condition,
> > big.list.of.columns], "foobar"))
> >
> > which would take the first argument of the inner function and assign
> > the function's result to it.
> >
> > Has anyone done something like this? Are there simple alternative
> > solutions that I'm missing?
> >
> > Cheers
> > David
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html