我有以下内容:
structure(list(label = list(list(structure(list(id = 431676528L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/per%20sviluppatori",
name = "per sviluppatori", color = "d4c5f9", default = FALSE), .Names = c("id",
"url", "name", "color", "default")), structure(list(id = 442034204L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale",
name = "sito principale", color = "5319e7", default = FALSE), .Names = c("id",
"url", "name", "color", "default"))), list(structure(list(id = 442051239L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/mappa",
name = "mappa", color = "0052cc", default = FALSE), .Names = c("id",
"url", "name", "color", "default")), structure(list(id = 431676528L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/per%20sviluppatori",
name = "per sviluppatori", color = "d4c5f9", default = FALSE), .Names = c("id",
"url", "name", "color", "default")), structure(list(id = 442034204L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale",
name = "sito principale", color = "5319e7", default = FALSE), .Names = c("id",
"url", "name", "color", "default")))), mainId = c("216226960",
"215647494")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-2L), .Names = c("label", "mainId")) -> dt
# A tibble: 2 x 2
label mainId
<list> <chr>
1 <list [2]> 216226960
2 <list [3]> 215647494
Run Code Online (Sandbox Code Playgroud)
我希望将这些值与label它们配对mainId(以便我可以将每个子元素label与其主ID 链接起来).因此,我试图让tibble与头:
label,url,name,color,mainId
我以为我会使用类似flatten_dfr()但我不知道要保留的东西mainId,主要是因为我不确定该论证flatten_dfr(.id='')是如何起作用的.
例如
dt %>% flatten_dfr(.id= 'mainId')
Run Code Online (Sandbox Code Playgroud)
bind_rows_(x,.id)出错:参数1必须有名称
什么是参数1?我猜它是,.x但我对这个不寻常的命名感到困惑.此外,rownames已被弃用,因此我对该函数所寻找的名称(colnames?rownames?)非常困惑.
更新:
这是输出 traceback()
> traceback()
13: stop(list(message = "Argument 1 must have names", call = bind_rows_(x,
.id), cppstack = list(file = "", line = -1L, stack = c("1 dplyr.so 0x000000010a9ce464 _ZN4Rcpp9exceptionC2EPKcb + 276",
"2 dplyr.so 0x000000010a9ce2a0 _ZN4Rcpp4stopERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE + 48",
"3 dplyr.so 0x000000010a9e5ae6 _ZN5dplyr11bad_pos_argIPKcEEviT_ + 918",
"4 dplyr.so 0x000000010a9fd43a _Z11rbind__implN4Rcpp6VectorILi19ENS_15PreserveStorageEEERKN5dplyr12SymbolStringE + 4634",
"5 dplyr.so 0x000000010a9ffcce _Z10bind_rows_N4Rcpp6VectorILi19ENS_15PreserveStorageEEEP7SEXPREC + 286",
"6 dplyr.so 0x000000010a9c28bc _dplyr_bind_rows_ + 140",
"7 libR.dylib 0x0000000101201b90 R_doDotCall + 64",
"8 libR.dylib 0x0000000101203db3 do_dotcall + 355",
"9 libR.dylib 0x0000000101232249 Rf_eval + 1657",
"10 libR.dylib 0x0000000101244a40 do_begin + 656",
"11 libR.dylib 0x0000000101231f78 Rf_eval + 936",
"12 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"13 libR.dylib 0x00000001012320f6 Rf_eval + 1318",
"14 libR.dylib 0x0000000101244a40 do_begin + 656",
"15 libR.dylib 0x0000000101231f78 Rf_eval + 936",
"16 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"17 libR.dylib 0x00000001012320f6 Rf_eval + 1318",
"18 libR.dylib 0x0000000101244a40 do_begin + 656",
"19 libR.dylib 0x0000000101231f78 Rf_eval + 936",
"20 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"21 libR.dylib 0x00000001012320f6 Rf_eval + 1318",
"22 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"23 libR.dylib 0x0000000101236384 bcEval + 16148",
"24 libR.dylib 0x0000000101231e11 Rf_eval + 577",
"25 libR.dylib 0x0000000101240949 forcePromise + 169",
"26 libR.dylib 0x0000000101232071 Rf_eval + 1185",
"27 libR.dylib 0x0000000101246761 do_withVisible + 49",
"28 libR.dylib 0x0000000101281e00 do_internal + 336",
"29 libR.dylib 0x0000000101236959 bcEval + 17641",
"30 libR.dylib 0x0000000101231e11 Rf_eval + 577",
"31 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"32 libR.dylib 0x0000000101236384 bcEval + 16148",
"33 libR.dylib 0x0000000101231e11 Rf_eval + 577",
"34 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"35 libR.dylib 0x00000001012320f6 Rf_eval + 1318",
"36 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"37 libR.dylib 0x00000001012320f6 Rf_eval + 1318",
"38 libR.dylib 0x00000001012460fe do_eval + 1534",
"39 libR.dylib 0x00000001012367ac bcEval + 17212",
"40 libR.dylib 0x0000000101231e11 Rf_eval + 577",
"41 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"42 libR.dylib 0x0000000101236384 bcEval + 16148",
"43 libR.dylib 0x0000000101231e11 Rf_eval + 577",
"44 libR.dylib 0x0000000101240949 forcePromise + 169",
"45 libR.dylib 0x0000000101232071 Rf_eval + 1185",
"46 libR.dylib 0x0000000101246761 do_withVisible + 49",
"47 libR.dylib 0x0000000101281e00 do_internal + 336",
"48 libR.dylib 0x0000000101236959 bcEval + 17641",
"49 libR.dylib 0x0000000101231e11 Rf_eval + 577",
"50 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"51 libR.dylib 0x0000000101236384 bcEval + 16148",
"52 libR.dylib 0x0000000101231e11 Rf_eval + 577",
"53 libR.dylib 0x00000001012428e2 R_execClosure + 898",
"54 libR.dylib 0x00000001012320f6 Rf_eval + 1318",
"55 libR.dylib 0x00000001012729b8 Rf_ReplIteration + 904",
"56 libR.dylib 0x0000000101273eaf run_Rmainloop + 207",
"57 rsession 0x000000010084e259 _ZN7rstudio1r7session12runEmbeddedRERKNS_4core8FilePathES5_bb7SA_TYPERKNS1_9CallbacksEPNS1_17InternalCallbacksE + 425",
"58 rsession 0x000000010082d25c _ZN7rstudio1r7session3runERKNS1_8ROptionsERKNS1_10RCallbacksE + 3164",
"59 rsession 0x0000000100108603 main + 28963",
"60 rsession 0x0000000100003264 start + 52",
"61 ??? 0x000000000000000b 0x0 + 11"
))))
12: .Call(`_dplyr_bind_rows_`, dots, id)
11: bind_rows_(x, .id)
10: dplyr::bind_rows(res, .id = .id)
9: flatten_dfr(., .id = "mainId")
8: function_list[[k]](value)
7: withVisible(function_list[[k]](value))
6: freduce(value, `_function_list`)
5: `_fseq`(`_lhs`)
4: eval(quote(`_fseq`(`_lhs`)), env, env)
3: eval(quote(`_fseq`(`_lhs`)), env, env)
2: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
1: dt %>% flatten_dfr(.id = "mainId")
Run Code Online (Sandbox Code Playgroud)
更新2:
到目前为止,解决方案可以解决我最初提供的示例,但无法处理NA.例如:
h <- structure(list(label = list(list(NA_character_)), mainId = "242390063"), .Names = c("label",
"mainId"), row.names = c(NA, -1L), class = c("tbl_df", "tbl",
"data.frame"))
map_df(h, flatten_dfr)
Run Code Online (Sandbox Code Playgroud)
bind_rows_(x,.id)出错:参数1必须有名称
我为没有包含NA在我原来的例子中而道歉,但也许有人可以详细说明他原来的答案来解决这个问题?
首先将label列中的数据转换为tibbles,然后使用它unnest来提取嵌套label列中的信息.
library(tidyverse)
dt %>%
mutate(label = map(label, bind_rows)) %>%
unnest()
# A tibble: 5 x 6
# mainId id url name color default
# <chr> <int> <chr> <chr> <chr> <lgl>
# 1 216226960 431676528 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/per%20sviluppatori per sviluppatori d4c5f9 F
# 2 216226960 442034204 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale sito principale 5319e7 F
# 3 215647494 442051239 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/mappa mappa 0052cc F
# 4 215647494 431676528 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/per%20sviluppatori per sviluppatori d4c5f9 F
# 5 215647494 442034204 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale sito principale 5319e7 F
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
867 次 |
| 最近记录: |