首页 > 解决方案 > R中的问题映射和未嵌套数据

问题描述

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

  1. 用 xml2 读取一个 xml 文件

  2. root_players 是一个 14 行 x 13 列的小标题

在此处输入图像描述

  1. node_players 是一个小标题列表

在此处输入图像描述

  1. 映射和取消列出时出错(如果我重新启动 R,此代码可以工作......但在运行其他代码后失败) 在此处输入图像描述

会话信息 在此处输入图像描述

查看代码


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

标签: rtidyversepurrrtibble

解决方案


推荐阅读