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

David Winsemius dwinsemius at comcast.net
Thu Jul 8 01:18:11 CEST 2010


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` means

It 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



More information about the R-help mailing list