purr map_df输出一个新的嵌套数据帧列
有没有办法添加一个额外的嵌套列,该列包含函数的新数据帧输出?下面是一个例子,我已经编写了一个函数,现在我正在尝试迭代每一行 这是一个函数,如果我在一组数据上运行它,它就会工作。(见a、b、c、d) 下面是我想要迭代的嵌套数据帧。我最初的尝试是使用PURR-map_-df,但现在我想知道是否应该为循环编写另一个forpurr map_df输出一个新的嵌套数据帧列,r,for-loop,tidyverse,purrr,R,For Loop,Tidyverse,Purrr,有没有办法添加一个额外的嵌套列,该列包含函数的新数据帧输出?下面是一个例子,我已经编写了一个函数,现在我正在尝试迭代每一行 这是一个函数,如果我在一组数据上运行它,它就会工作。(见a、b、c、d) 下面是我想要迭代的嵌套数据帧。我最初的尝试是使用PURR-map_-df,但现在我想知道是否应该为循环编写另一个for structure(list(OBJECTID_1 = c(170795, 158926, 170796, 170797, 74758, 170798, 74757, 71331,
structure(list(OBJECTID_1 = c(170795, 158926, 170796, 170797,
74758, 170798, 74757, 71331, 158748, 158800, 171144, 167991,
170985, 159202, 167990), data = list(structure(list(X_1 = 791806.957864181,
X_2 = 785512.771698002, Y_1 = 233314.224607777, Y_2 = 229184.215067145), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 792533.074659662, X_2 = 783388.018236045, Y_1 = 230885.419496296,
Y_2 = 224878.340874981), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 795052.843843351,
X_2 = 785643.485631476, Y_1 = 229406.40394036, Y_2 = 223245.75510431), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 796821.226335759, X_2 = 787145.416317165, Y_1 = 227462.665657252,
Y_2 = 221047.564227364), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 795356.971998954,
X_2 = 791651.414871993, Y_1 = 237855.746923772, Y_2 = 233539.238149352), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 787145.416317165, X_2 = 796821.226335759, Y_1 = 221047.564227364,
Y_2 = 227462.665657252), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 798885.441403441,
X_2 = 792816.47413827, Y_1 = 237907.774432991, Y_2 = 230870.388411334), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 801886.200413522, X_2 = 795052.843843351, Y_1 = 237384.986466147,
Y_2 = 229406.40394036), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 782215.495007085,
X_2 = 778004.911567101, Y_1 = 229531.311160664, Y_2 = 226740.660699846), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 774111.10739776, X_2 = 779461.875017808, Y_1 = 221345.75680274,
Y_2 = 221361.262444083), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 779461.875017808,
X_2 = 774111.10739776, Y_1 = 221361.262444083, Y_2 = 221345.75680274), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 779284.987142645, X_2 = 785357.019122782, Y_1 = 225436.143812854,
Y_2 = 229420.355663708), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 785357.019122782,
X_2 = 779284.987142645, Y_1 = 229420.355663708, Y_2 = 225436.143812854), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 784672.158689655, X_2 = 784708.07793811, Y_1 = 221376.364048245,
Y_2 = 216070.684445299), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 784708.07793811,
X_2 = 784672.158689655, Y_1 = 216070.684445299, Y_2 = 221376.364048245), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)))), row.names = c(NA,
-15L), groups = structure(list(OBJECTID_1 = c(71331, 74757, 74758,
158748, 158800, 158926, 159202, 167990, 167991, 170795, 170796,
170797, 170798, 170985, 171144), .rows = structure(list(8L, 7L,
5L, 9L, 10L, 2L, 14L, 15L, 12L, 1L, 3L, 4L, 6L, 13L, 11L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 15L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
这就是我用map_df试过的
simplepolys_filtered_nest %>%
mutate(df2= ~map_df(.,parallel_spacing_fn(X_1,X_2,Y_1,Y_2)))
simplepolys_filtered_nest %>%
mutate(df2= ~map_dfr(.,parallel_spacing_fn(X_1,X_2,Y_1,Y_2)))
谢谢你的帮助 这就是您想要的:
df %>%
unnest_wider(data) %>%
mutate(res=pmap(list(X_1, X_2, Y_1, Y_2), parallel_spacing_fn)) %>%
nest(X_1:Y_2)
# A tibble: 15 x 3
# Groups: OBJECTID_1 [15]
OBJECTID_1 res data
<dbl> <list> <list>
1 170795 <df[,4] [36 x 4]> <tibble [1 x 4]>
2 158926 <df[,4] [36 x 4]> <tibble [1 x 4]>
3 170796 <df[,4] [36 x 4]> <tibble [1 x 4]>
4 170797 <df[,4] [36 x 4]> <tibble [1 x 4]>
5 74758 <df[,4] [36 x 4]> <tibble [1 x 4]>
6 170798 <df[,4] [36 x 4]> <tibble [1 x 4]>
7 74757 <df[,4] [36 x 4]> <tibble [1 x 4]>
8 71331 <df[,4] [36 x 4]> <tibble [1 x 4]>
9 158748 <df[,4] [36 x 4]> <tibble [1 x 4]>
10 158800 <df[,4] [36 x 4]> <tibble [1 x 4]>
11 171144 <df[,4] [36 x 4]> <tibble [1 x 4]>
12 167991 <df[,4] [36 x 4]> <tibble [1 x 4]>
13 170985 <df[,4] [36 x 4]> <tibble [1 x 4]>
14 159202 <df[,4] [36 x 4]> <tibble [1 x 4]>
15 167990 <df[,4] [36 x 4]> <tibble [1 x 4]>
df%>%
unnest_加宽(数据)%>%
突变(res=pmap(列表(X_1,X_2,Y_1,Y_2),平行间距(u fn))%>%
嵌套(X_1:Y_2)
#一个tibble:15x3
#组别:OBJECTID_1[15]
OBJECTID_1 res数据
1 170795
2 158926
3 170796
4 170797
5 74758
6 170798
7 74757
8 71331
9 158748
10 158800
11 171144
12 167991
13 170985
14 159202
15 167990
确实有一种更优雅的方法可以访问列表列的元素,而不必在应用函数之前取消对列表列的测试,但我觉得这种方法很清楚。非常感谢!正是我想要的。
structure(list(OBJECTID_1 = c(170795, 158926, 170796, 170797,
74758, 170798, 74757, 71331, 158748, 158800, 171144, 167991,
170985, 159202, 167990), data = list(structure(list(X_1 = 791806.957864181,
X_2 = 785512.771698002, Y_1 = 233314.224607777, Y_2 = 229184.215067145), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 792533.074659662, X_2 = 783388.018236045, Y_1 = 230885.419496296,
Y_2 = 224878.340874981), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 795052.843843351,
X_2 = 785643.485631476, Y_1 = 229406.40394036, Y_2 = 223245.75510431), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 796821.226335759, X_2 = 787145.416317165, Y_1 = 227462.665657252,
Y_2 = 221047.564227364), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 795356.971998954,
X_2 = 791651.414871993, Y_1 = 237855.746923772, Y_2 = 233539.238149352), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 787145.416317165, X_2 = 796821.226335759, Y_1 = 221047.564227364,
Y_2 = 227462.665657252), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 798885.441403441,
X_2 = 792816.47413827, Y_1 = 237907.774432991, Y_2 = 230870.388411334), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 801886.200413522, X_2 = 795052.843843351, Y_1 = 237384.986466147,
Y_2 = 229406.40394036), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 782215.495007085,
X_2 = 778004.911567101, Y_1 = 229531.311160664, Y_2 = 226740.660699846), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 774111.10739776, X_2 = 779461.875017808, Y_1 = 221345.75680274,
Y_2 = 221361.262444083), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 779461.875017808,
X_2 = 774111.10739776, Y_1 = 221361.262444083, Y_2 = 221345.75680274), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 779284.987142645, X_2 = 785357.019122782, Y_1 = 225436.143812854,
Y_2 = 229420.355663708), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 785357.019122782,
X_2 = 779284.987142645, Y_1 = 229420.355663708, Y_2 = 225436.143812854), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
X_1 = 784672.158689655, X_2 = 784708.07793811, Y_1 = 221376.364048245,
Y_2 = 216070.684445299), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), structure(list(X_1 = 784708.07793811,
X_2 = 784672.158689655, Y_1 = 216070.684445299, Y_2 = 221376.364048245), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -1L)))), row.names = c(NA,
-15L), groups = structure(list(OBJECTID_1 = c(71331, 74757, 74758,
158748, 158800, 158926, 159202, 167990, 167991, 170795, 170796,
170797, 170798, 170985, 171144), .rows = structure(list(8L, 7L,
5L, 9L, 10L, 2L, 14L, 15L, 12L, 1L, 3L, 4L, 6L, 13L, 11L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 15L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
simplepolys_filtered_nest %>%
mutate(df2= ~map_df(.,parallel_spacing_fn(X_1,X_2,Y_1,Y_2)))
simplepolys_filtered_nest %>%
mutate(df2= ~map_dfr(.,parallel_spacing_fn(X_1,X_2,Y_1,Y_2)))
df %>%
unnest_wider(data) %>%
mutate(res=pmap(list(X_1, X_2, Y_1, Y_2), parallel_spacing_fn)) %>%
nest(X_1:Y_2)
# A tibble: 15 x 3
# Groups: OBJECTID_1 [15]
OBJECTID_1 res data
<dbl> <list> <list>
1 170795 <df[,4] [36 x 4]> <tibble [1 x 4]>
2 158926 <df[,4] [36 x 4]> <tibble [1 x 4]>
3 170796 <df[,4] [36 x 4]> <tibble [1 x 4]>
4 170797 <df[,4] [36 x 4]> <tibble [1 x 4]>
5 74758 <df[,4] [36 x 4]> <tibble [1 x 4]>
6 170798 <df[,4] [36 x 4]> <tibble [1 x 4]>
7 74757 <df[,4] [36 x 4]> <tibble [1 x 4]>
8 71331 <df[,4] [36 x 4]> <tibble [1 x 4]>
9 158748 <df[,4] [36 x 4]> <tibble [1 x 4]>
10 158800 <df[,4] [36 x 4]> <tibble [1 x 4]>
11 171144 <df[,4] [36 x 4]> <tibble [1 x 4]>
12 167991 <df[,4] [36 x 4]> <tibble [1 x 4]>
13 170985 <df[,4] [36 x 4]> <tibble [1 x 4]>
14 159202 <df[,4] [36 x 4]> <tibble [1 x 4]>
15 167990 <df[,4] [36 x 4]> <tibble [1 x 4]>