R 映射函数中的第二个参数

R 映射函数中的第二个参数,r,dplyr,purrr,R,Dplyr,Purrr,我有一个像这样的tibble: uuu <- structure(list(Id = c("a", "b"), lists = list(c(1:2), c(100:103)) ), .Names = c("Id", "lists"), row.names = c(NA, 2L), class = c("tbl_df", "tbl", "data.frame")) > uuu # A tibble: 2 x 2 Id lists

我有一个像这样的tibble:

uuu <- structure(list(Id = c("a", "b"),
                      lists = list(c(1:2), c(100:103))
),
.Names = c("Id", "lists"),
row.names = c(NA, 2L), class = c("tbl_df", "tbl", "data.frame"))

> uuu
# A tibble: 2 x 2
  Id    lists    
* <chr> <list>   
1 a     <int [2]>
2 b     <int [4]>
> unlist(res$res)
[1]  1.000000  1.414214 10.000000 10.049876 10.099505 10.148892
res <- uuu %>% mutate(res = map(lists, my_sqrt, id = id))
my_sqrt <- function(x, id) return(sqrt(x), id)
res
现在有一个新列,其中包含
sqrt
函数的所有结果

现在我想在
res
列中取消列出结果,但我还想有一个新列,指示结果来自哪个
id

我可以这样取消列出结果:

uuu <- structure(list(Id = c("a", "b"),
                      lists = list(c(1:2), c(100:103))
),
.Names = c("Id", "lists"),
row.names = c(NA, 2L), class = c("tbl_df", "tbl", "data.frame"))

> uuu
# A tibble: 2 x 2
  Id    lists    
* <chr> <list>   
1 a     <int [2]>
2 b     <int [4]>
> unlist(res$res)
[1]  1.000000  1.414214 10.000000 10.049876 10.099505 10.148892
res <- uuu %>% mutate(res = map(lists, my_sqrt, id = id))
my_sqrt <- function(x, id) return(sqrt(x), id)
但我真正想要的是这样的东西(这里是手动完成的示例):


我们可以使用
unest

library(tidyverse)
res %>% 
  select(-lists) %>%
  unnest