如何在列表列中创建新列?我只能通过改变成一个新的列表列来实现。
此外,我发现与其他方法相比,下面的双重突变非常慢。 mutate会增加很多开销吗?
library(repurrrsive)
library(tidyverse)
#> ── Attaching packages ─────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1 ✔ purrr 0.2.4
#> ✔ tibble 1.4.2 ✔ dplyr 0.7.4
#> ✔ tidyr 0.8.0 ✔ stringr 1.3.0
#> ✔ readr 1.1.1 ✔ forcats 0.3.0
#> ── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
system.time(rep(
gap_m<-gap_nested %>%
mutate(new_list=
map(data,~ .x %>% mutate(pop_str=str_sub(pop,end = 4))))))
#> user system elapsed
#> 0.087 0.005 0.091
system.time(gap_c<-gap_nested %>%
mutate(new_list=
map(data,~ cbind(.x,pop_str=str_sub(.x$pop,end=4),stringsAsFactors=F))))
#> user system elapsed
#> 0.017 0.000 0.017
system.time(gap_b<-gap_nested %>%
mutate(new_list=
map(data,~ bind_cols(.x,pop_str=str_sub(.x$pop,end=4)))))
#> user system elapsed
#> 0.02 0.00 0.02
答案 0 :(得分:0)
执行您的操作,但将结果保存回data
:
mutate(.df, data = map(data, .f))