[R] What does `_data` mean in transform()?

thmsfuller066 at gmail.com thmsfuller066 at gmail.com
Thu Jul 8 00:32:32 CEST 2010


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.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



More information about the R-help mailing list