假设我有以下数据框和列表
import 'package:http/http.dart' as http;
...
final response = await http.get(Url);
我想在列表中找到# data frames
df1 = data.frame(y1 = c(1:5), y2 = c(8:12))
df2 = data.frame(y1 = c(5:8), y2 = c(9:12))
df3 = data.frame(y1 = c(5:8), y2 = c(9:12))
df4 = data.frame(y1 = c(11:15), y2 = c(16:20))
# list of data.frames
my.list <- list(df1, df2, df3, df4)
的位置,我尝试了一下,但得到了df1
。我也尝试过NA
,但遇到错误。
==
但是当我这样做时,我会得到结果
match(df1, my.list)
# [1] NA NA
我相信它与列表中的list14 = my.list[c(1,4)]
match(list14, my.list)
# [1] 1 4
和[]
标记有关。最终我想做类似的事情
[[]]
其中for (i in list14) {
cbind(list14, "indexPositionOf_list14_in_my.list")
}
是indexPositionOf_list14_in_my.list
中list14
中相应data.frame的索引位置。
my.list
循环的预期输出
for
答案 0 :(得分:1)
也许是这样吗?
my.list<- mapply(cbind, my.list, id=seq_along(my.list), SIMPLIFY = FALSE)
my.list %>%
map(., inner_join, df3) %>%
map(., compact) %>%
compact()
#> Joining, by = c("y1", "y2")
#> Joining, by = c("y1", "y2")
#> Joining, by = c("y1", "y2")
#> Joining, by = c("y1", "y2")
#> [[1]]
#> y1 y2 id
#> 1 5 9 2
#> 2 6 10 2
#> 3 7 11 2
#> 4 8 12 2
#>
#> [[2]]
#> y1 y2 id
#> 1 5 9 3
#> 2 6 10 3
#> 3 7 11 3
#> 4 8 12 3
答案 1 :(得分:1)
您可以使用duplicated
和mapply
:
mapply(cbind, my.list, id=seq_along(my.list), SIMPLIFY = FALSE)[duplicated(my.list) | duplicated(my.list, fromLast = TRUE)]
[[1]]
y1 y2 id
1 5 9 2
2 6 10 2
3 7 11 2
4 8 12 2
[[2]]
y1 y2 id
1 5 9 3
2 6 10 3
3 7 11 3
4 8 12 3
library(rbenchmark)
benchmark("baseR" = {
mapply(cbind, my.list, id=seq_along(my.list), SIMPLIFY = FALSE)[duplicated(my.list) | duplicated(my.list, fromLast = TRUE)]
},
"map" = {
mapply(cbind, my.list, id=seq_along(my.list), SIMPLIFY = FALSE) %>%
map(., inner_join, df3) %>%
map(., compact) %>%
compact()
},
replications = 1000,
columns = c("test", "replications", "elapsed",
"relative", "user.self", "sys.self"))
test replications elapsed relative user.self
1 baseR 1000 0.37 1.000 0.38
2 map 1000 4.82 13.027 4.80
sys.self
1 0.00
2 0.02