首页 > 解决方案 > Rstudio,试图将数据从 EML.R 提取到表中,但出现错误:错误:必须命名第 1、2、3、4、5 和 18 列

问题描述

我对 R 很陌生,我正在尝试遵循教程。

以下是他们希望我们遵循的步骤。

# INSTALL AND LOAD PACKAGES ################################

# Load contributed packages with pacman
pacman::p_load(pacman, tidyverse, XML2R)
# pacman: for loading/unloading packages
# tidyverse: for so many reasons
# XML2R: for working with XML data

# GET DATA & RESTRUCTURE ###################################

# Data from http://ergast.com/mrd/
# File: http://ergast.com/api/f1/1954/results/1.xml
# Right click to "View page source" and see raw XML

# Import XML data from web (must be online)
df <- "http://ergast.com/api/f1/1954/results/1.xml" %>%
  XML2Obs() %>%
  collapse_obs() %>%
  print()

# See variable (node) names and IDs
df %>%
  names() %>%
  print()

# EXTRACT & COMBINE DATA ###################################

# Extract data and save as tibble
df %<>%  # %<>% is the compound assignment pipe operator
  as_tibble() %>%                        # Save as tibble
  select(                                # Select data
    Race = matches("RaceName"),          # Get race name
    FirstName = matches("GivenName"),    # Get first name
    LastName = matches("FamilyName"),    # Get last name
    Team = matches("Constructor//Name")  # Get team name
  ) %>% 
  print()  # Each column is a list with two values (oops)

我正在为“提取和合并数据”这一步而苦苦挣扎

我收到错误消息:错误:第 1、2、3、4、5 和 18 列必须命名。

我尝试了一些选项,例如 .name_repair ,但它不起作用。

有任何想法吗?

标签: rtibble

解决方案


推荐阅读