英文:
Iterating a pipe over objects in a list
问题
我有一系列的数据框,我想使用管道运行几个函数。我创建了一个对象列表如下(实际上大约有30个对象):
a <- 1:10
b <- 5:14
c <- 10:19
d <- c(NA, 4:12)
e <- 60:69
f <- 7:16
d1 <- tibble(a, b, c, d, e)
d2 <- tibble(a, b, c, e, f)
d_list <- list(d1, d2)
然后我尝试使用for
循环在数据框上进行函数管道操作:
for(d in d_list){
d %>%
select(c(1, 3:5)) %>%
na.omit()
}
如果我在循环之外的一个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):
a <- 1:10
b <- 5:14
c <- 10:19
d <- c(NA, 4:12)
e <- 60:69
f <- 7:16
d1 <- tibble(a, b, c, d, e)
d2 <- tibble(a, b, c, e, f)
d_list <- c(d1, d2)
Then I tried to use a for
loop to pipe functions over the data frames:
for(d in d_list){
d %>%
select(c(1, 3:5)) %>%
na.omit()
}
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
:
library(dplyr)
a <- 1:10
b <- 5:14
c <- 10:19
d <- c(NA, 4:12)
e <- 60:69
f <- 7:16
d1 <- tibble(a, b, c, d, e)
d2 <- tibble(a, b, c, e, f)
d_list <- c(d1, d2)
d_list
如果使用 list()
替代,您可以在其上使用 lapply()
:
d_list <- list(d1, d2)
d_list <- lapply(d_list, function(x) {
x %>%
select(c(1, 3:5)) %>%
na.omit()
})
d_list
希望这有帮助。
英文:
You need to call list()
instead of c()
. c()
concatenates the two tibble
s in a strange way:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
a <- 1:10
b <- 5:14
c <- 10:19
d <- c(NA, 4:12)
e <- 60:69
f <- 7:16
d1 <- tibble(a, b, c, d, e)
d2 <- tibble(a, b, c, e, f)
d_list <- c(d1, d2)
d_list
#> $a
#> [1] 1 2 3 4 5 6 7 8 9 10
#>
#> $b
#> [1] 5 6 7 8 9 10 11 12 13 14
#>
#> $c
#> [1] 10 11 12 13 14 15 16 17 18 19
#>
#> $d
#> [1] NA 4 5 6 7 8 9 10 11 12
#>
#> $e
#> [1] 60 61 62 63 64 65 66 67 68 69
#>
#> $a
#> [1] 1 2 3 4 5 6 7 8 9 10
#>
#> $b
#> [1] 5 6 7 8 9 10 11 12 13 14
#>
#> $c
#> [1] 10 11 12 13 14 15 16 17 18 19
#>
#> $e
#> [1] 60 61 62 63 64 65 66 67 68 69
#>
#> $f
#> [1] 7 8 9 10 11 12 13 14 15 16
If you use list()
instead, you can use lapply()
on it:
d_list <- list(d1, d2)
d_list <- lapply(d_list, function(x) {
x %>%
select(c(1, 3:5)) %>%
na.omit()
})
d_list
#> [[1]]
#> # A tibble: 9 × 4
#> 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
#> 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
答案2
得分: 3
请注意,以下是翻译好的部分:
一旦您已解决@bretauv答案中标识的问题(使用`list(d1, d2)`而不是`c(d1, d2)`),然后尝试:
d_list |>
lapply(select, c(1,3:5)) |>
lapply(na.omit)
`lapply`接受一个列表作为输入,对每个元素应用指定的函数(带参数),然后将结果作为列表返回。
输出是一个列表:
一个数据框:9 × 4
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
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
<details>
<summary>英文:</summary>
Once you've fixed the issue identified in @bretauv's answer (`list(d1, d2)` instead of `c(d1, d2)`) then try:
d_list |>
lapply(select, c(1,3:5)) |>
lapply(na.omit)
`lapply` takes a list as input, applies the specified function (with arguments) to each element and returns the results as a list.
Output is a list:
A tibble: 9 × 4
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
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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论