问题映射和R中的嵌套数据

时间:2020-10-04 17:10:31

标签: r tidyverse purrr tibble

我遇到一个奇怪的行为(如果我重新启动Rstudio,此代码可以正常运行,但是如果我在另一个运行此错误的脚本运行后运行)。

  1. 使用xml2读取xml文件

  2. root_players是一小块14行x 13列

enter image description here

  1. node_players是一些小贴士

enter image description here

  1. 映射和取消列出列表时出错(如果我重新启动R,此代码有效...但是运行其他代码后失败) enter image description here

会话信息 enter image description here

查看代码


library(xml2)
library(tidyverse)


xml_filename <-"file.xml"
  
  
# Leer XML
datos <- read_xml(xml_filename)

meta_datos <- datos %>%
  xml_attrs() %>% 
  t() %>% 
  as_tibble()


root_players <- xml_find_all(datos, '/SoccerFeed/Player') %>%
  xml_attrs() %>%
  map(~as_tibble(t(.))) %>% 
  bind_rows()


node_players <- xml_find_all(datos, '/SoccerFeed/Player') %>% 
  map(~.x %>% 
        xml_children() %>% 
        map(function(.y) {.y %>% 
            xml_attrs() %>% 
            t() %>% 
            as_tibble() %>% 
            mutate(n_passes = xml_text(.y))}) %>% 
        bind_rows())

#############################
# HERE ERROR APPEARS
############################


root_players <- root_players %>% 
  mutate(n = map(node_players, count) %>% unlist()) %>% 
  uncount(weights = n)


#########################
### ERROR TRACE
#########################


Error: Assigned data `eval(cols[[col]], .data, parent.frame())` must be compatible with existing data.
x Existing data has 14 rows.
x Assigned data has 363 rows.
i Only vectors of size 1 are recycled.


<error/tibble_error_assign_incompatible_size>
Assigned data `eval(cols[[col]], .data, parent.frame())` must be compatible with existing data.
x Existing data has 14 rows.
x Assigned data has 363 rows.
i Only vectors of size 1 are recycled.
Backtrace:
Run `rlang::last_trace()` to see the full context

0 个答案:

没有答案