thmsfuller066 at gmail.com
2010-Jul-07 22:32 UTC
[R] What does `_data` mean in transform()?
Hi All, I meant to take the min row by row. But the result is apparently not what I want. Changing min to pmin solve the problem.> df=data.frame(X=1:10, Y=1:10) > transform(df, Z=min(X,10-Y))X Y Z 1 1 1 0 2 2 2 0 3 3 3 0 4 4 4 0 5 5 5 0 6 6 6 0 7 7 7 0 8 8 8 0 9 9 9 0 10 10 10 0 I try to look at the source code to understand what transform() does. I know that ` can be used to refer to a column in a data.frame. But I don't understand what the usage of `_data` in transform.data.frame. Could you please help me understand what `_data` means and min doesn't work row by row?> transform.data.framefunction (`_data`, ...) { e <- eval(substitute(list(...)), `_data`, parent.frame()) tags <- names(e) inx <- match(tags, names(`_data`)) matched <- !is.na(inx) if (any(matched)) { `_data`[inx[matched]] <- e[matched] `_data` <- data.frame(`_data`) } if (!all(matched)) do.call("data.frame", c(list(`_data`), e[!matched])) else `_data` } <environment: namespace:base> -- Tom
On Jul 7, 2010, at 6:32 PM, thmsfuller066 at gmail.com wrote:> Hi All, > > I meant to take the min row by row. But the result is apparently not > what I want. Changing min to pmin solve the problem. > >> df=data.frame(X=1:10, Y=1:10) >> transform(df, Z=min(X,10-Y)) > X Y Z > 1 1 1 0 > 2 2 2 0 > 3 3 3 0 > 4 4 4 0 > 5 5 5 0 > 6 6 6 0 > 7 7 7 0 > 8 8 8 0 > 9 9 9 0 > 10 10 10 0 > > I try to look at the source code to understand what transform() does. > I know that ` can be used to refer to a column in a data.frame. But I > don't understand what the usage of `_data` in transform.data.frame. > Could you please help me understand what `_data` meansIt is a function argument that gets assigned a fvalue after a function call.> and min doesn't > work row by row??pmin> > >> transform.data.frame > function (`_data`, ...) > { > e <- eval(substitute(list(...)), `_data`, parent.frame()) > tags <- names(e) > inx <- match(tags, names(`_data`)) > matched <- !is.na(inx) > if (any(matched)) { > `_data`[inx[matched]] <- e[matched] > `_data` <- data.frame(`_data`) > } > if (!all(matched)) > do.call("data.frame", c(list(`_data`), e[!matched])) > else `_data` > } > <environment: namespace:base> > > > -- > Tom-- David Winsemius, MD West Hartford, CT
thmsfuller066 at gmail.com wrote:> Could you please help me understand what `_data` means and min doesn't > work row by row?`_data` is just a name (once upon a time, it was called "x" but then someone wanted to create a new variable called x). min() calculates the scalar minimum of everything it gets. It doesn't work by row, because that's what pmin() does.... (It's a design choice, you just have to believe that it is intentional and that min(X,Y) =min(c(X,Y)) has useful applications). -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Seemingly Similar Threads
- transform.data.frame() ignores unnamed arguments when no named argument is provided
- transform(_data,...) using strptime gives an error
- transform.data.frame() ignores unnamed arguments when no named argument is provided
- Renaming columns of a data.frame
- Bug in "transform"?