嵌套替换运算符如何工作?

发布于 2024-12-11 00:43:52 字数 845 浏览 0 评论 0原文

我今天正在处理一个列表列表,需要替换其中一个二级列表的元素。做到这一点的方法似乎很明显,但我意识到我实际上并不清楚它为什么有效。

这是一个例子:

a <- list(aa=list(aaa=1:10,bbb=11:20,ccc=21:30),bb=list(ddd=1:5))

给定这个数据结构,假设我想替换嵌套数值向量 aaa 的第三个元素。我可以这样做:

newvalue <- 100
a$aa$aaa[3] <- newvalue

这样做似乎很明显,但我无法向自己解释这个表达式实际上是如何被求值的。使用 quote 函数,我拼凑了一些粗略的逻辑,大致如下:

(1) 创建并提交顶级函数调用:

`<-`(a$aa$aaa[3],newvalue)  

(2) 对 (1) 中的第一个参数进行惰性求值,调用函数“[”:

`[`(a$aa$aaa,3)    

(3) 继续向下递归:

`$`(a$aa,"aaa")

(4) ...再往下,再次调用 '$':

`$`(a,"aa")

(5) 当 (4) 返回一个实际的数据结构时,继续“向上堆栈”替换返回的数据结构,直到实际的数据结构被返回。分配是在(1).

我想我的困惑涉及惰性评估和/或评估环境的某些方面。在上面的示例中,我只是重新分配了向量的一个元素。但是 R 如何跟踪该向量在更大的数据结构中的位置呢?

干杯

I was working with a list of lists today and needed to replace an element of one of the second-level lists. The way to do this seemed obvious, but I realized I wasn't actually clear on why it worked.

Here's an example:

a <- list(aa=list(aaa=1:10,bbb=11:20,ccc=21:30),bb=list(ddd=1:5))

Given this data structure, let's say I want to replace the 3rd element of the nested numeric vector aaa. I could do something like:

newvalue <- 100
a$aa$aaa[3] <- newvalue

Doing this seems obvious enough, but I couldn't explain to myself how this expression actually gets evaluated. Working with the quote function, I cobbled together some rough logic, along the lines of:

(1) Create and submit the top-level function call:

`<-`(a$aa$aaa[3],newvalue)  

(2) Lazy evaluation of first argument in (1), call function '[':

`[`(a$aa$aaa,3)    

(3) Proceed recursivley down:

`

I was working with a list of lists today and needed to replace an element of one of the second-level lists. The way to do this seemed obvious, but I realized I wasn't actually clear on why it worked.

Here's an example:

a <- list(aa=list(aaa=1:10,bbb=11:20,ccc=21:30),bb=list(ddd=1:5))

Given this data structure, let's say I want to replace the 3rd element of the nested numeric vector aaa. I could do something like:

newvalue <- 100
a$aa$aaa[3] <- newvalue

Doing this seems obvious enough, but I couldn't explain to myself how this expression actually gets evaluated. Working with the quote function, I cobbled together some rough logic, along the lines of:

(1) Create and submit the top-level function call:

`<-`(a$aa$aaa[3],newvalue)  

(2) Lazy evaluation of first argument in (1), call function '[':

`[`(a$aa$aaa,3)    

(3) Proceed recursivley down:

(a$aa,"aaa")

(4) ...further down, call '$' again:

`

I was working with a list of lists today and needed to replace an element of one of the second-level lists. The way to do this seemed obvious, but I realized I wasn't actually clear on why it worked.

Here's an example:

a <- list(aa=list(aaa=1:10,bbb=11:20,ccc=21:30),bb=list(ddd=1:5))

Given this data structure, let's say I want to replace the 3rd element of the nested numeric vector aaa. I could do something like:

newvalue <- 100
a$aa$aaa[3] <- newvalue

Doing this seems obvious enough, but I couldn't explain to myself how this expression actually gets evaluated. Working with the quote function, I cobbled together some rough logic, along the lines of:

(1) Create and submit the top-level function call:

`<-`(a$aa$aaa[3],newvalue)  

(2) Lazy evaluation of first argument in (1), call function '[':

`[`(a$aa$aaa,3)    

(3) Proceed recursivley down:

`

I was working with a list of lists today and needed to replace an element of one of the second-level lists. The way to do this seemed obvious, but I realized I wasn't actually clear on why it worked.

Here's an example:

a <- list(aa=list(aaa=1:10,bbb=11:20,ccc=21:30),bb=list(ddd=1:5))

Given this data structure, let's say I want to replace the 3rd element of the nested numeric vector aaa. I could do something like:

newvalue <- 100
a$aa$aaa[3] <- newvalue

Doing this seems obvious enough, but I couldn't explain to myself how this expression actually gets evaluated. Working with the quote function, I cobbled together some rough logic, along the lines of:

(1) Create and submit the top-level function call:

`<-`(a$aa$aaa[3],newvalue)  

(2) Lazy evaluation of first argument in (1), call function '[':

`[`(a$aa$aaa,3)    

(3) Proceed recursivley down:

(a$aa,"aaa")

(4) ...further down, call '$' again:

(a,"aa")

(5) With (4) returning an actual data structure, proceed back "up the stack" substituting the returned data structures until the actual assignment is made in (1).

I guess my confusion involves some aspects of lazy evaluation and/or evaluation environments. In the example above, I've simply reassigned one element of a vector. But how is it that R keeps track of where that vector is within the greater data structure?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-12-18 00:43:52

我认为 a$aa$aaa[3] 的工作方式如下:

当您想访问仍在当前对象内的元素时,您可以使用单方括号 []。这使得该元素可以访问,但您无法使用它执行复杂的操作,因为该元素仍然是对象的一部分。

当您使用 $ 访问该元素时,它会从 a$aa 转换为 a[["aa"]],从而从当前对象。

总表达式 a$aa$aaa[3] 转换为 a[["aa"]][["aaa"]][3]。这被视为向量的向量->

  1. 获取对象a,访问向量aa
  2. 访问向量aaa
  3. 访问向量aaa中的第三个元素。

R评估:

 > a
 $aa
 $aa$aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $aa$bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $aa$ccc
 [1] 21 22 23 24 25 26 27 28 29 30


 $bb
 $bb$ddd
 [1] 1 2 3 4 5


 > a$aa
 $aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $ccc
 [1] 21 22 23 24 25 26 27 28 29 30

 > a$aa$aaa
 [1]  1  2  3  4  5  6  7  8  9 10
 > a[["aa"]]
 $aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $ccc
 [1] 21 22 23 24 25 26 27 28 29 30

 > a[["aa"]][["aaa"]]
 [1]  1  2  3  4  5  6  7  8  9 10
 > a["aa"]
 $aa
 $aa$aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $aa$bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $aa$ccc
 [1] 21 22 23 24 25 26 27 28 29 30


 > a["aa"]["aaa"]
 
lt;NA>
 NULL

希望这有帮助。

I think a$aa$aaa[3] works as followed:

When you want to access an element still within the current object you use single square brackets, []. This makes the element accesible, but you can not perform complex manipulations with it because the element is still part of the object.

When you access the element using $, this translates from a$aa to a[["aa"]], thus releasing the element from the current object.

The total expression a$aa$aaa[3] translates to a[["aa"]][["aaa"]][3]. This is treated as a vector of vectors ->

  1. Take object a, access vector aa.
  2. Access vector aaa.
  3. Access the third element in vector aaa.

The R evaluation:

 > a
 $aa
 $aa$aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $aa$bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $aa$ccc
 [1] 21 22 23 24 25 26 27 28 29 30


 $bb
 $bb$ddd
 [1] 1 2 3 4 5


 > a$aa
 $aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $ccc
 [1] 21 22 23 24 25 26 27 28 29 30

 > a$aa$aaa
 [1]  1  2  3  4  5  6  7  8  9 10
 > a[["aa"]]
 $aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $ccc
 [1] 21 22 23 24 25 26 27 28 29 30

 > a[["aa"]][["aaa"]]
 [1]  1  2  3  4  5  6  7  8  9 10
 > a["aa"]
 $aa
 $aa$aaa
 [1]  1  2  3  4  5  6  7  8  9 10

 $aa$bbb
 [1] 11 12 13 14 15 16 17 18 19 20

 $aa$ccc
 [1] 21 22 23 24 25 26 27 28 29 30


 > a["aa"]["aaa"]
 
lt;NA>
 NULL

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文