`transform`和`within`有什么区别

时间:2022-11-12 12:05:59

Reading this excellent post i came across within and transform.

阅读这篇优秀的文章,我发现并改造。

Reading both help files i unfortunatly did not fully understand what the differnence is...

阅读这两个帮助文件我不幸的是没有完全理解差异是什么......

I tried something like:

我试过类似的东西:

df <- data.frame(A = runif(5), B = rnorm(5))
A=1:5
within(df, C<-A+B)
transform(df,C=A+B)

Both times the Output was:

两次输出都是:

          A          B         C
1 0.2326266  1.3237210 1.5563476
2 0.4581693 -0.2605674 0.1976018
3 0.6431078  0.5920021 1.2351099
4 0.9682578  1.1964012 2.1646590
5 0.9889942  0.5468008 1.5357950

So both seem to create a new enwironment as they are ignoring A=1:5 within the evaluation.

所以两者似乎都创造了一种新的环境,因为他们在评估中忽略了A = 1:5。

Thanks in advance!

提前致谢!

1 个解决方案

#1


13  

within lets you use an earlier defined variable later but not transform:

允许您稍后使用先前定义的变量,但不能转换:

within(BOD, { a <- demand; b <- a }) # ok
transform(BOD, a = demand, b = a) # error

Note that I had defined a variation of transform that acts more like within a number of years ago here where it is called my.transform. Using that we could write the above like this:

请注意,我已经定义了一个变换的变体,它在很多年前的行为更像是my.transform。使用它我们可以像这样写上面的内容:

my.transform(BOD, a = demand, b = a) # ok

In the above examples within (or my.transform) would be better but in the following transform is better:

在上面的例子中(或my.transform)会更好,但在下面的转换中更好:

transform(BOD, Time = demand, demand = Time) # swap columns
within(BOD, { Time <- demand; demand <- Time }) # oops

(To perform the swap with within would require that we define a temporary.)

(要在内部执行交换,需要我们定义一个临时的。)

EDIT

my.transform is now in the gsubfn CRAN package where it is called transform2 . mutate in dplyr works from left to right.

my.transform现在位于gsubfn CRAN包中,称为transform2。 dplyr中的mutate从左到右工作。

Note that transform, transform2 and mutate each work slightly differently. The RHS transform arguments all refer to the original values. The RHS of mutate arguments refer to the most recent left-to-right value. transform2 figures out the dependencies and uses those so that a dependent can come before or after the argument in which it used.

请注意,transform,transform2和mutate的每个工作略有不同。 RHS变换参数都是指原始值。 mutate参数的RHS指的是最近的从左到右的值。 transform2计算出依赖关系并使用它们,以便依赖关系可以在它使用的参数之前或之后出现。

#1


13  

within lets you use an earlier defined variable later but not transform:

允许您稍后使用先前定义的变量,但不能转换:

within(BOD, { a <- demand; b <- a }) # ok
transform(BOD, a = demand, b = a) # error

Note that I had defined a variation of transform that acts more like within a number of years ago here where it is called my.transform. Using that we could write the above like this:

请注意,我已经定义了一个变换的变体,它在很多年前的行为更像是my.transform。使用它我们可以像这样写上面的内容:

my.transform(BOD, a = demand, b = a) # ok

In the above examples within (or my.transform) would be better but in the following transform is better:

在上面的例子中(或my.transform)会更好,但在下面的转换中更好:

transform(BOD, Time = demand, demand = Time) # swap columns
within(BOD, { Time <- demand; demand <- Time }) # oops

(To perform the swap with within would require that we define a temporary.)

(要在内部执行交换,需要我们定义一个临时的。)

EDIT

my.transform is now in the gsubfn CRAN package where it is called transform2 . mutate in dplyr works from left to right.

my.transform现在位于gsubfn CRAN包中,称为transform2。 dplyr中的mutate从左到右工作。

Note that transform, transform2 and mutate each work slightly differently. The RHS transform arguments all refer to the original values. The RHS of mutate arguments refer to the most recent left-to-right value. transform2 figures out the dependencies and uses those so that a dependent can come before or after the argument in which it used.

请注意,transform,transform2和mutate的每个工作略有不同。 RHS变换参数都是指原始值。 mutate参数的RHS指的是最近的从左到右的值。 transform2计算出依赖关系并使用它们,以便依赖关系可以在它使用的参数之前或之后出现。