在列表中迭代管道上的对象

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

Iterating a pipe over objects in a list

问题

我有一系列的数据框,我想使用管道运行几个函数。我创建了一个对象列表如下(实际上大约有30个对象):

  1. a <- 1:10
  2. b <- 5:14
  3. c <- 10:19
  4. d <- c(NA, 4:12)
  5. e <- 60:69
  6. f <- 7:16
  7. d1 <- tibble(a, b, c, d, e)
  8. d2 <- tibble(a, b, c, e, f)
  9. d_list <- list(d1, d2)

然后我尝试使用for循环在数据框上进行函数管道操作:

  1. for(d in d_list){
  2. d %>%
  3. select(c(1, 3:5)) %>%
  4. na.omit()
  5. }

如果我在循环之外的一个tibble上运行管道,它可以正常工作。但是在循环内部,我收到一个错误:Error in UseMethod("select") :
no applicable method for 'select' applied to an object of class "c('integer', 'numeric')". 我猜这意味着解析器不理解我想要将函数应用于d_list中的数据框;但我不明白为什么。

谢谢,

Peter

英文:

I have a series of data frames, and I want to run several functions on them using a pipe. I created a list of the objects as follows (in reality there are around 30 objects):

  1. a &lt;- 1:10
  2. b &lt;- 5:14
  3. c &lt;- 10:19
  4. d &lt;- c(NA, 4:12)
  5. e &lt;- 60:69
  6. f &lt;- 7:16
  7. d1 &lt;- tibble(a, b, c, d, e)
  8. d2 &lt;- tibble(a, b, c, e, f)
  9. d_list &lt;- c(d1, d2)

Then I tried to use a for loop to pipe functions over the data frames:

  1. for(d in d_list){
  2. d %&gt;%
  3. select(c(1, 3:5)) %&gt;%
  4. na.omit()
  5. }

If I run the pipeline on one of the tibbles outside of the for loop, it works fine. But inside the loop I get an error: Error in UseMethod("select") :
no applicable method for 'select' applied to an object of class "c('integer', 'numeric')". I guess this means that the parser does not understand that I want to apply the functions to the data frames in d_list; but I don't understand why.

Thanks,

Peter

答案1

得分: 3

需要调用 list() 而不是 c()c() 以一种奇怪的方式连接了两个 tibble

  1. library(dplyr)
  2. a <- 1:10
  3. b <- 5:14
  4. c <- 10:19
  5. d <- c(NA, 4:12)
  6. e <- 60:69
  7. f <- 7:16
  8. d1 <- tibble(a, b, c, d, e)
  9. d2 <- tibble(a, b, c, e, f)
  10. d_list <- c(d1, d2)
  11. d_list

如果使用 list() 替代,您可以在其上使用 lapply()

  1. d_list <- list(d1, d2)
  2. d_list <- lapply(d_list, function(x) {
  3. x %>%
  4. select(c(1, 3:5)) %>%
  5. na.omit()
  6. })
  7. d_list

希望这有帮助。

英文:

You need to call list() instead of c(). c() concatenates the two tibbles in a strange way:

  1. library(dplyr)
  2. #&gt;
  3. #&gt; Attaching package: &#39;dplyr&#39;
  4. #&gt; The following objects are masked from &#39;package:stats&#39;:
  5. #&gt;
  6. #&gt; filter, lag
  7. #&gt; The following objects are masked from &#39;package:base&#39;:
  8. #&gt;
  9. #&gt; intersect, setdiff, setequal, union
  10. a &lt;- 1:10
  11. b &lt;- 5:14
  12. c &lt;- 10:19
  13. d &lt;- c(NA, 4:12)
  14. e &lt;- 60:69
  15. f &lt;- 7:16
  16. d1 &lt;- tibble(a, b, c, d, e)
  17. d2 &lt;- tibble(a, b, c, e, f)
  18. d_list &lt;- c(d1, d2)
  19. d_list
  20. #&gt; $a
  21. #&gt; [1] 1 2 3 4 5 6 7 8 9 10
  22. #&gt;
  23. #&gt; $b
  24. #&gt; [1] 5 6 7 8 9 10 11 12 13 14
  25. #&gt;
  26. #&gt; $c
  27. #&gt; [1] 10 11 12 13 14 15 16 17 18 19
  28. #&gt;
  29. #&gt; $d
  30. #&gt; [1] NA 4 5 6 7 8 9 10 11 12
  31. #&gt;
  32. #&gt; $e
  33. #&gt; [1] 60 61 62 63 64 65 66 67 68 69
  34. #&gt;
  35. #&gt; $a
  36. #&gt; [1] 1 2 3 4 5 6 7 8 9 10
  37. #&gt;
  38. #&gt; $b
  39. #&gt; [1] 5 6 7 8 9 10 11 12 13 14
  40. #&gt;
  41. #&gt; $c
  42. #&gt; [1] 10 11 12 13 14 15 16 17 18 19
  43. #&gt;
  44. #&gt; $e
  45. #&gt; [1] 60 61 62 63 64 65 66 67 68 69
  46. #&gt;
  47. #&gt; $f
  48. #&gt; [1] 7 8 9 10 11 12 13 14 15 16

If you use list() instead, you can use lapply() on it:

  1. d_list &lt;- list(d1, d2)
  2. d_list &lt;- lapply(d_list, function(x) {
  3. x %&gt;%
  4. select(c(1, 3:5)) %&gt;%
  5. na.omit()
  6. })
  7. d_list
  8. #&gt; [[1]]
  9. #&gt; # A tibble: 9 &#215; 4
  10. #&gt; a c d e
  11. #&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  12. #&gt; 1 2 11 4 61
  13. #&gt; 2 3 12 5 62
  14. #&gt; 3 4 13 6 63
  15. #&gt; 4 5 14 7 64
  16. #&gt; 5 6 15 8 65
  17. #&gt; 6 7 16 9 66
  18. #&gt; 7 8 17 10 67
  19. #&gt; 8 9 18 11 68
  20. #&gt; 9 10 19 12 69
  21. #&gt;
  22. #&gt; [[2]]
  23. #&gt; # A tibble: 10 &#215; 4
  24. #&gt; a c e f
  25. #&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  26. #&gt; 1 1 10 60 7
  27. #&gt; 2 2 11 61 8
  28. #&gt; 3 3 12 62 9
  29. #&gt; 4 4 13 63 10
  30. #&gt; 5 5 14 64 11
  31. #&gt; 6 6 15 65 12
  32. #&gt; 7 7 16 66 13
  33. #&gt; 8 8 17 67 14
  34. #&gt; 9 9 18 68 15
  35. #&gt; 10 10 19 69 16

答案2

得分: 3

请注意,以下是翻译好的部分:

  1. 一旦您已解决@bretauv答案中标识的问题(使用`list(d1, d2)`而不是`c(d1, d2)`),然后尝试:

d_list |>
lapply(select, c(1,3:5)) |>
lapply(na.omit)

  1. `lapply`接受一个列表作为输入,对每个元素应用指定的函数(带参数),然后将结果作为列表返回。
  2. 输出是一个列表:

一个数据框:9 × 4

  1. a c d e


1 2 11 4 61
2 3 12 5 62
3 4 13 6 63
4 5 14 7 64
5 6 15 8 65
6 7 16 9 66
7 8 17 10 67
8 9 18 11 68
9 10 19 12 69

[[2]]

一个数据框:10 × 4

  1. a c e f


1 1 10 60 7
2 2 11 61 8
3 3 12 62 9
4 4 13 63 10
5 5 14 64 11
6 6 15 65 12
7 7 16 66 13
8 8 17 67 14
9 9 18 68 15
10 10 19 69 16

  1. <details>
  2. <summary>英文:</summary>
  3. Once you&#39;ve fixed the issue identified in @bretauv&#39;s answer (`list(d1, d2)` instead of `c(d1, d2)`) then try:

d_list |>
lapply(select, c(1,3:5)) |>
lapply(na.omit)

  1. `lapply` takes a list as input, applies the specified function (with arguments) to each element and returns the results as a list.
  2. Output is a list:

A tibble: 9 × 4

  1. a c d e

<int> <int> <int> <int>
1 2 11 4 61
2 3 12 5 62
3 4 13 6 63
4 5 14 7 64
5 6 15 8 65
6 7 16 9 66
7 8 17 10 67
8 9 18 11 68
9 10 19 12 69

[[2]]

A tibble: 10 × 4

  1. a c e f

<int> <int> <int> <int>
1 1 10 60 7
2 2 11 61 8
3 3 12 62 9
4 4 13 63 10
5 5 14 64 11
6 6 15 65 12
7 7 16 66 13
8 8 17 67 14
9 9 18 68 15
10 10 19 69 16

  1. </details>

huangapple
  • 本文由 发表于 2023年6月5日 22:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407399.html
匿名

发表评论

匿名网友

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

确定