提取在lapply中应用函数的列表的名称

huangapple go评论96阅读模式
英文:

extracting the names of a lists on which we are applying a function in lapply

问题

我想提取在lapply()函数内应用函数的列表元素的名称。编辑:也许这不太清楚,为了澄清一下,必须在lapply()中完成。

以下是一个可重现的示例,以便澄清:

  1. A <- c("z", "y", "x", "v")
  2. B <- c("q", "j", "k", "r")
  3. C <- c("n", "e", "d", "f")
  4. myList <- list(A = A, B = B, C = C)
  5. names_list <- lapply(myList, function(x) {
  6. ???(x)
  7. })
  8. output: "A" "B" "C"

我不确定如何完成这个任务,我在网上找不到任何信息。我最接近期望输出的方法是使用envnames包中的get_obj_name函数,但它给了我以下结果:

  1. $A
  2. [1] "x"
  3. $B
  4. [1] "x"
  5. $C
  6. [1] "x"

谢谢你的帮助。

英文:

I want to extract the names of the elements of a list on which I am applying a function within the lapply(). EDIT: perhaps this was not clear so just to clarify it has to be done in lapply().

Here is a reproducible example to clarify.

  1. A &lt;- c(&quot;z&quot;, &quot;y&quot;, &quot;x&quot;, &quot;v&quot;)
  2. B &lt;- c(&quot;q&quot;, &quot;j&quot;, &quot;k&quot;, &quot;r&quot;)
  3. C &lt;- c(&quot;n&quot;, &quot;e&quot;, &quot;d&quot;, &quot;f&quot;)
  4. myList &lt;- list(A = A,B = B, C = C)
  5. names_list &lt;- lapply(myList, function(x) {
  6. ???(x)
  7. })
  8. output: &quot;A&quot; &quot;B&quot; &quot;C&quot;

I am not sure how this could be done, and I cannot find any information online. The closest I have come to the desired output is by using

get_obj_name from the envnames package but it gives me

  1. $A
  2. [1] &quot;x&quot;
  3. $B
  4. [1] &quot;x&quot;
  5. $C
  6. [1] &quot;x&quot;

Thank you for your help.

答案1

得分: 2

  1. 使用lapply处理列表的名称而不是列表本身。 使用tibble包中的lst形成列表,因为它会自动添加名称到列表中。
  2. library(tibble)
  3. L <- lst(A, B, C) # lst创建列表并添加名称
  4. res <- lapply(names(L), function(nm) {
  5. x <- L[[nm]]
  6. print(nm)
  7. print(x)
  8. nm
  9. })
  10. 另一种方法是同时传递名称和值:
  11. res2 <- Map(function(nm, x) {
  12. print(nm)
  13. print(x)
  14. nm
  15. }, names(L), L)
英文:

lapply over the list names rather than over the list itself. Form the list
using lst from the tibble package as that will automatically add names to the
list.

  1. library(tibble)
  2. L &lt;- lst(A, B, C) # lst creates list and adds names
  3. res &lt;- lapply(names(L), function(nm) {
  4. x &lt;- L[[nm]]
  5. print(nm)
  6. print(x)
  7. nm
  8. })

Another approach is to pass both the names and the values:

  1. res2 &lt;- Map(function(nm, x) {
  2. print(nm)
  3. print(x)
  4. nm
  5. }, names(L), L)

答案2

得分: 1

你的列表没有名称,你可以这样编程,然后就不需要lapply了。

  1. A <- c("z", "y", "x", "v")
  2. B <- c("q", "j", "k", "r")
  3. C <- c("n", "e", "d", "f")
  4. myList <- list(A=A, B=B, C=C)
  5. names_list <- lapply(names(myList), function(x) {print(x)})
  6. names_list
英文:

Your lists do not have names with how you programmed it. You'd have to code it like this and then you would not need the lapply

  1. A &lt;- c(&quot;z&quot;, &quot;y&quot;, &quot;x&quot;, &quot;v&quot;)
  2. B &lt;- c(&quot;q&quot;, &quot;j&quot;, &quot;k&quot;, &quot;r&quot;)
  3. C &lt;- c(&quot;n&quot;, &quot;e&quot;, &quot;d&quot;, &quot;f&quot;)
  4. myList &lt;- list(A=A, B=B, C=C)
  5. names_list &lt;- lapply(names(myList), function(x) {print(x)})
  6. names_list

huangapple
  • 本文由 发表于 2023年7月7日 01:58:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定