【发布时间】:2019-08-17 02:19:29
【问题描述】:
我想使用map_dfr 和group_split 通过while 循环运行一组data.frame 并存储结果。
我可以为这样的一个小组做到这一点。
# df dput below
# this code finds the closet match for DIFF for Sample.x in Sample.y, then finds the next closest match, until
df_f <- df %>% filter(grp == "AB" & VAR == "Var1")
HowMany <- length(unique(df_f$Sample.y))
i <- 1
MyList <- list()
while (i <= HowMany){
res1 <- df_f %>%
group_by(grp, VAR, Sample.x) %>%
filter(DIFF == min(DIFF)) %>%
ungroup() %>%
mutate(Rank1 = dense_rank(DIFF))
res2 <- res1 %>% group_by(grp, VAR) %>% filter(rank(Rank1, ties.method="first")==1)
SY <- as.numeric(res2$Sample.y)
SX <- as.numeric(res2$Sample.x)
res3 <- df_f %>% filter(Sample.y != SY)
res4 <- res3 %>% filter(Sample.x != SX)
df_f <- res4
MyList[[i]] <- res2
i <- i + 1
}
df.result <- do.call("rbind", MyList)
但是,当尝试使用 while 循环创建函数以与 map_dfr 和 group_split 一起使用时,我无法和/或不确定如何存储输出。
MyResult <- df %>%
dplyr::group_split(grp, VAR) %>%
map_dfr(fun) # fun below
df.store <- data.frame() # attempt to store results
fun <- function(df){
HowMany <- length(unique(df$Sample.y))
i <- 1
MyList_FF <- list()
ThisDF <- df
while (i <= HowMany){
res1 <- ThisDF %>%
group_by(grp, VAR, Sample.x) %>%
filter(DIFF == min(DIFF)) %>%
ungroup() %>%
mutate(Rank1 = dense_rank(DIFF))
res2 <- res1 %>% group_by(grp, VAR) %>% filter(rank(Rank1, ties.method="first")==1)
# print(res2) # when printed to screen the desired output looks correct
SY <- as.numeric(res2$Sample.y)
SX <- as.numeric(res2$Sample.x)
res3 <- ThisDF %>% filter(Sample.y != SY)
res4 <- res3 %>% filter(Sample.x != SX)
# df.store <- rbind(df.store, res4)
# MyList_FF[[i]] <- res2
ThisDF <- res4
i <- i + 1
}
}
我尝试使用rbind 或使用list 来存储输出,但我的尝试并不正确。如果我在屏幕上打印“res2”,我可以一次看到所需的输出一行。如何存储来自每个group_split 的fun 的输出?
# df dput
df <- structure(list(Location.x = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("A", "C", "B"), class = "factor"),
Sample.x = c(6L, 6L, 10L, 10L, 9L, 9L, 6L, 6L, 10L, 10L,
9L, 9L, 6L, 6L, 6L, 10L, 10L, 10L, 9L, 9L, 9L, 6L, 6L, 6L,
10L, 10L, 10L, 9L, 9L, 9L, 1L, 1L, 1L, 9L, 9L, 9L, 1L, 1L,
1L, 9L, 9L, 9L), VAR = c("Var1", "Var1", "Var1", "Var1",
"Var1", "Var1", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2",
"Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1",
"Var1", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2", "Var2",
"Var2", "Var2", "Var1", "Var1", "Var1", "Var1", "Var1", "Var1",
"Var2", "Var2", "Var2", "Var2", "Var2", "Var2"), value.x = c(56.48,
56.48, 57.03, 57.03, 55.04, 55.04, 6, 6, 10, 10, 9, 9, 56.48,
56.48, 56.48, 57.03, 57.03, 57.03, 55.04, 55.04, 55.04, 6,
6, 6, 10, 10, 10, 9, 9, 9, 55.62, 55.62, 55.62, 55.65, 55.65,
55.65, 1, 1, 1, 9, 9, 9), Location.y = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A",
"C", "B"), class = "factor"), Sample.y = c(1L, 9L, 1L, 9L,
1L, 9L, 1L, 9L, 1L, 9L, 1L, 9L, 3L, 7L, 9L, 3L, 7L, 9L, 3L,
7L, 9L, 3L, 7L, 9L, 3L, 7L, 9L, 3L, 7L, 9L, 3L, 7L, 9L, 3L,
7L, 9L, 3L, 7L, 9L, 3L, 7L, 9L), value.y = c(55.62, 55.65,
55.62, 55.65, 55.62, 55.65, 1, 9, 1, 9, 1, 9, 1.4, 111.6,
111.8, 1.4, 111.6, 111.8, 1.4, 111.6, 111.8, 10.2, 14.4,
20.9, 10.2, 14.4, 20.9, 10.2, 14.4, 20.9, 1.4, 111.6, 111.8,
1.4, 111.6, 111.8, 10.2, 14.4, 20.9, 10.2, 14.4, 20.9), DIFF = c(0.859999999999999,
0.829999999999998, 1.41, 1.38, 0.579999999999998, 0.609999999999999,
5, 3, 9, 1, 8, 0, 55.08, 55.12, 55.32, 55.63, 54.57, 54.77,
53.64, 56.56, 56.76, 4.2, 8.4, 14.9, 0.199999999999999, 4.4,
10.9, 1.2, 5.4, 11.9, 54.22, 55.98, 56.18, 54.25, 55.95,
56.15, 9.2, 13.4, 19.9, 1.2, 5.4, 11.9), grp = c("AC", "AC",
"AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC",
"AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB",
"AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "CB", "CB",
"CB", "CB", "CB", "CB", "CB", "CB", "CB", "CB", "CB", "CB"
)), row.names = c(NA, -42L), class = "data.frame")
【问题讨论】:
-
如果您已经习惯使用
map函数,为什么不使用嵌套的maps 而不是while循环? -
我的
map舒适程度在我上面提供的示例中。我需要探索嵌套的maps。