R中的和R中的有什么区别?

时间:2021-05-12 13:20:19

I always use "with" instead of "within" within the context of my research, but I originally thought they were the same. Just now I mistype "with" for "within" and the results returned are quite different. I am wondering why?

在我的研究中,我总是使用“with”而不是“within”,但我最初认为它们是一样的。刚才我用“with”来表示“within”,结果返回的结果是完全不同的。我想知道为什么?

I am using the baseball data in the plyr package, so I first load the library by

我正在plyr包中使用棒球数据,所以我首先通过

 require(plyr)

Then, I want to select all rows with an id "ansonca01". At first, as I said, I used "within", and run the function as follows:

然后,我要选择具有id“ansonca01”的所有行。首先,如我所说,我使用了“within”,并运行如下函数:

within(baseball, baseball[id=="ansonca01", ])

I got very strange results which basically includes everything:

我得到了非常奇怪的结果,基本上包括了所有的东西:

       id year stint team lg   g  ab   r   h X2b X3b hr rbi  sb cs  bb  so ibb hbp sh sf gidp
4     ansonca01 1871     1  RC1     25 120  29  39  11   3  0  16   6  2   2   1  NA  NA NA NA   NA
44    forceda01 1871     1  WS3     32 162  45  45   9   4  0  29   8  0   4   0  NA  NA NA NA   NA
68    mathebo01 1871     1  FW1     19  89  15  24   3   1  0  10   2  1   2   0  NA  NA NA NA   NA
99    startjo01 1871     1  NY2     33 161  35  58   5   1  1  34   4  2   3   0  NA  NA NA NA   NA
102   suttoez01 1871     1  CL1     29 128  35  45   3   7  3  23   3  1   1   0  NA  NA NA NA   NA
106   whitede01 1871     1  CL1     29 146  40  47   6   5  1  21   2  2   4   1  NA  NA NA NA   NA
113    yorkto01 1871     1  TRO     29 145  36  37   5   7  2  23   2  2   9   1  NA  NA NA NA   NA
.........

Then I use "with" instead of "within",

然后我用with而不是within,

 with(baseball, baseball[id=="ansonca01",])

and got the results that I expected

得到了我期望的结果

      id year stint team lg   g  ab   r   h X2b X3b hr rbi sb cs  bb so ibb hbp sh sf gidp
4    ansonca01 1871     1  RC1     25 120  29  39  11   3  0  16  6  2   2  1  NA  NA NA NA   NA
121  ansonca01 1872     1  PH1     46 217  60  90  10   7  0  50  6  6  16  3  NA  NA NA NA   NA
276  ansonca01 1873     1  PH1     52 254  53 101   9   2  0  36  0  2   5  1  NA  NA NA NA   NA
398  ansonca01 1874     1  PH1     55 259  51  87   8   3  0  37  6  0   4  1  NA  NA NA NA   NA
525  ansonca01 1875     1  PH1     69 326  84 106  15   3  0  58 11  6   4  2  NA  NA NA NA   NA

I checked the documentation of with and within by typing help(with) in R environment, and got the following:

我在R环境中输入help(with)检查了with和within的文档,得到了以下信息:

with is a generic function that evaluates expr in a local environment constructed from data. The environment has the caller's environment as its parent. This is useful for simplifying calls to modeling functions. (Note: if data is already an environment then this is used with its existing parent.)

with是一个泛型函数,它在由数据构造的本地环境中计算expr。环境将调用者的环境作为其父环境。这对于简化对建模函数的调用非常有用。(注意:如果数据已经是一个环境,那么它将与它现有的父环境一起使用。)

Note that assignments within expr take place in the constructed environment and not in the user's workspace.

注意,expr中的分配发生在构建的环境中,而不是用户的工作空间中。

within is similar, except that it examines the environment after the evaluation of expr and makes the corresponding modifications to data (this may fail in the data frame case if objects are created which cannot be stored in a data frame), and returns it. within can be used as an alternative to transform.

内部是类似的,除了它在expr评估之后检查环境,并对数据进行相应的修改(如果创建的对象不能存储在数据帧中,这可能会在数据框架中失败),并返回它。内部可以作为转换的替代。

From this explanation of the differences, I don't get why I obtained different results with such a simple operation. Anyone has ideas?

从这个差异的解释中,我不明白为什么我用这么简单的操作得到了不同的结果。谁有想法吗?

2 个解决方案

#1


7  

The documentation is quite clear about the semantics and return values (and nicely matches the everyday meanings of the words “with” and “within”):

文档对语义和返回值非常清楚(并且很好地匹配了单词“with”和“within”的日常含义):

Value:

值:

For ‘with’, the value of the evaluated ‘expr’. For ‘within’, the modified object.

对于“with”,值为“expr”。对于“within”,修改后的对象。

Since your code doesn’t modify anything inside baseball, the unmodified baseball is returned. with on the other hand doesn’t return the object, it returns expr.

由于代码不修改棒球内部的任何内容,因此返回未修改的棒球。另一方面,它不返回对象,而是返回expr。

Here’s an example where the expression modifies the object:

这里有一个表达式修改对象的例子:

> head(within(cars, speed[dist < 20] <- 1))
  speed dist
1     1    2
2     1   10
3     1    4
4     7   22
5     1   16
6     1   10

#2


12  

I find simple examples often work to highlight the difference. Something like:

我发现简单的例子往往能突出差异。喜欢的东西:

df <- data.frame(a=1:5,b=2:6)
df
  a b
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6

with(df, {c <- a + b; df;} )
  a b
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6

within(df, {c <- a + b; df;} )
# equivalent to: within(df, c <- a + b)
# i've just made the return of df explicit 
# for comparison's sake
  a b  c
1 1 2  3
2 2 3  5
3 3 4  7
4 4 5  9
5 5 6 11

#1


7  

The documentation is quite clear about the semantics and return values (and nicely matches the everyday meanings of the words “with” and “within”):

文档对语义和返回值非常清楚(并且很好地匹配了单词“with”和“within”的日常含义):

Value:

值:

For ‘with’, the value of the evaluated ‘expr’. For ‘within’, the modified object.

对于“with”,值为“expr”。对于“within”,修改后的对象。

Since your code doesn’t modify anything inside baseball, the unmodified baseball is returned. with on the other hand doesn’t return the object, it returns expr.

由于代码不修改棒球内部的任何内容,因此返回未修改的棒球。另一方面,它不返回对象,而是返回expr。

Here’s an example where the expression modifies the object:

这里有一个表达式修改对象的例子:

> head(within(cars, speed[dist < 20] <- 1))
  speed dist
1     1    2
2     1   10
3     1    4
4     7   22
5     1   16
6     1   10

#2


12  

I find simple examples often work to highlight the difference. Something like:

我发现简单的例子往往能突出差异。喜欢的东西:

df <- data.frame(a=1:5,b=2:6)
df
  a b
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6

with(df, {c <- a + b; df;} )
  a b
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6

within(df, {c <- a + b; df;} )
# equivalent to: within(df, c <- a + b)
# i've just made the return of df explicit 
# for comparison's sake
  a b  c
1 1 2  3
2 2 3  5
3 3 4  7
4 4 5  9
5 5 6 11