Question: I was wondering if there was anything like the `subset` function, but for assignment rather than extraction. Often I'll need to do something like

```
rows <- # some condition
df$x[rows] <- with(df[rows, ], {
    # operation 1...
})
df$y[rows] <- with(df[rows, ], {
    # operation 2...
})

```

And it seems to me that it would be nice to be able to write

```
subset(df, rows, c(x, y)) <- (some expression combining operations 1 and 2)

```

Is there anything like this out there?

EDIT: Some background. In SAS, one can write data processing code like

```
if /* condition */ then do;
    x = ...; y = ...; z = ...;
end;
else if /* some other condition */ then do;
    x = ...; y = ...; z = ...;
end;
else if /* etcetera */

```

I'm basically looking for the easiest/most elegant way to replicate this in R. The direct translation would involve a for loop over all rows in the data frame, and obviously I'd rather not do that.

Answer: 
Remember that `subset` is only the sugar on top. As such, why not use 'basic' subsetting as provided by `[`:

```
df[rows, c("x", "y")]<-with(df[rows,], {...})

```

Does that cut it for you?
Regarding the SAS-ish processing, if the order of the rows is not relevant:

1. Add a column to your data.frame that holds the different cases as
a factor. You could e.g. use an `ifelse` for this, but depending on
the conditions, there may be better ways. I'll assume this column is
now named "proctype"
2. Load `plyr`, and use `ddply` something akin to:

```
ddply(df, .(proctype), function(curdf){
    #you can use with/within here with curdf
    curproctype <- curdf$proctype[1]
    switch(curproctype, #note: may be easier to just use an if here
       nameoffirstproctypelevel = firstkindofprocessing(curdf),
       nameofsecondproctypelevel = secondkindofprocessing(curdf))
})

```