映射嵌套列表时dplyr::select报NULL错误的排查与解决
R嵌套列表字段提取报错求助
之前用类似代码处理小嵌套列表能正常工作,现在处理包含89个元素的大嵌套列表时,同一逻辑的代码抛出错误。
嵌套列表结构
str(df_raw_comments_threads) List of 89 $ tgg001 :List of 2 ..$ comments:'data.frame': 992 obs. of 12 variables: .. ..$ date_utc : chr [1:992] "2019-02-05" "2019-02-05" "2019-02-05" "2019-02-05" ... .. ..$ timestamp : num [1:992] 1.55e+09 1.55e+09 1.55e+09 1.55e+09 1.55e+09 ... .. ..$ subreddit : chr [1:992] "hardwareswap" "hardwareswap" "hardwareswap" "hardwareswap" ... .. ..$ thread_author : chr [1:992] "aagarwal82" "aagarwal82" "aagarwal82" "[deleted]" ... .. ..$ comment_author: chr [1:992] "tgg001" "tgg001" "tgg001" "tgg001" ... .. ..$ thread_title : chr [1:992] "[USA-NC] [H] NVIDIA GTX 1080 TI MSI Duke [W] PayPal" "[USA-NC] [H] NVIDIA GTX 1080 TI MSI Duke [W] PayPal" "[USA-NC] [H] NVIDIA GTX 1080 TI MSI Duke [W] PayPal" "[USA-VA] [H] G.Skill Trident Z RGB 32GB 4 x 8GB DDR4-3200, Enermax Liqtech TR4 II 280mm, Asus X399 Zenith Extre"| __truncated__ ... .. ..$ comment : chr [1:992] "Yes I paid for it" "SOLD TO ME W\017" "check pm" "Well I need the other 16gb.... OP can we split this?" ... .. ..$ score : num [1:992] 1 1 1 1 1 1 1 1 1 2 ... .. ..$ up : num [1:992] 1 1 1 1 1 1 1 1 1 2 ... .. ..$ downs : num [1:992] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ golds : num [1:992] 0 0 0 0 0 0 0 0 0 0 ... ..$ threads :'data.frame': 62 obs. of 11 variables: .. ..$ date_utc : chr [1:62] "2019-01-22" "2019-01-21" "2019-01-21" "2018-11-29" ... .. ..$ timestamp: num [1:62] 1.55e+09 1.55e+09 1.55e+09 1.54e+09 1.54e+09 ... .. ..$ subreddit: chr [1:62] "FortniteCompetitive" "FortNiteBR" "FortNiteBR" "Kanye" ... .. ..$ author : chr [1:62] "tgg001" "tgg001" "tgg001" "tgg001" ... .. ..$ title : chr [1:62] "Well rip my 4th consecutive solo win streak today. he had 1 kill and 50 health... clearly the better player" "15 kills later basically solo squad and this is how I die. Epic can we vault trees?" "I went AFK for 20 seconds and I came back at the perfect time lol" "=«>â Is he wavy? >\024 If not try to roast him with Kanye lyric-related comments" ... .. ..$ text : chr [1:62] "" "" "" "" ... .. ..$ golds : num [1:62] 0 0 0 0 0 0 0 0 0 0 ... .. ..$ score : num [1:62] 0 3289 3115 0 41 ... .. ..$ ups : num [1:62] 0 3289 3115 0 41 ... .. ..$ downs : num [1:62] 0 0 0 0 0 0 0 0 0 0 ...
需求
从每个列表元素下的comments和threads两个数据框中,提取subreddit和date_utc字段,最终保留原嵌套结构。
第一次尝试的代码及错误
df_map_date_sub <- map2( df_raw_comments_threads |> map(~ .x$comments |> dplyr::select("subreddit", "date_utc")), df_raw_comments_threads |> map(~ .x$threads |> dplyr::select("subreddit", "date_utc")), ~ list(comments = .x, threads = .y) )
错误信息:
Error in UseMethod("select") : no applicable method for 'select' applied to an object of class "NULL"
排查尝试
- 重启R,问题依旧
- 检查列表中是否存在NULL值,执行以下代码:
group_errs = df_map_comments_threads %>% map(~ .x$comments |> keep(~is.null(.x) ) ) %>% names() > length(group_errs) [1] 89
结果显示有89个元素,这让我很困惑,不知道问题出在哪。
第二次尝试的代码及错误
换了另一种写法,还是报错:
df_map_date_sub <- map2( df_raw_comments_threads |> map(.x$comments, ~ dplyr::select(.,subreddit, date_utc)), df_raw_comments_threads |> map(.x$threads, ~ dplyr::select(.,subreddit, date_utc)), ~ list(comments = .x, threads = .y) )
错误信息:
Error in as_mapper(.f, ...) : object '.x' not found
内容的提问来源于stack exchange,提问作者LuCama
相关产品推荐
相关产品推荐

