[英]how to add a column to a netsed tibble in R
tbl_a <- tibble( a = letters[1:3], b = 1:3 ) tbl_b <- tibble( c = letters[1:3], d = 1:3 ) tbl_nested <- tibble( name = c("Test", "Sand"), nest_tbl = list(tbl_a,tbl_b) )
最終嵌套的小標題應該是:
tbl_a_new <- tibble( name = "Test", a = letters[1:3], b = 1:3 ) tbl_b_new <- tibble( name = "San", a = letters[1:3], b = 1:3 ) tibble( name = c("Test", "Sand"), nest_tbl = list(tbl_a,tbl_b), nest_tbl_new = list(tbl_a_new,tbl_b_new) ) name nest_tbl nest_tbl_new <chr> <list> <list> 1 Test <tibble [3 x 2]> <tibble [3 x 3]> 2 Sand <tibble [3 x 2]> <tibble [3 x 3]>
非常感謝您的幫助
我們可以使用map2
library(dplyr)
library(purrr)
tbl_nested %>%
mutate(nest_tbl_new = map2(nest_tbl, name, ~
.x %>%
mutate(name = .y)))
-輸出
# A tibble: 2 x 3
# name nest_tbl nest_tbl_new
# <chr> <list> <list>
#1 Test <tibble [3 × 2]> <tibble [3 × 3]>
#2 Sand <tibble [3 × 2]> <tibble [3 × 3]>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.