Разбор Excel XML в R - PullRequest
       57

Разбор Excel XML в R

0 голосов
/ 25 сентября 2019

Привет, я знаю, что для этого есть много пакетов, но ни один из них не работал над этим моим конкретным файлом.

Мне нужно проанализировать этот файл из XML во фрейм данных.Он должен игнорировать все форматирование и просто дать мне мои данные.

Я пробовал пакеты XML, XML2, OPENXLSX, TIDYXL, READR и XLSX, и никто не может это сделать.Либо они говорят, что это не файл XLS / XLXS или беспорядок данных

library(XML)
fileurl <- "pep_slim.xml"
doc <- xmlParse(fileurl,useInternalNodes = TRUE) 
doc
xL <- xmlToList(doc)
data <- ldply(xL, data.frame)
head(data)

Полный файл находится ниже и заканчивается * .xml

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Author>Proctor, Stephon N</Author>
  <LastAuthor>Stephon Proctor</LastAuthor>
  <Created>2019-09-25T13:30:11Z</Created>
  <Version>16.00</Version>
 </DocumentProperties>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>10590</WindowHeight>
  <WindowWidth>25440</WindowWidth>
  <WindowTopX>0</WindowTopX>
  <WindowTopY>0</WindowTopY>
  <RefModeR1C1/>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#4F81BD" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s63">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#4FBD81" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s64">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#A081BD" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s65">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#D85647" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s66">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#FF9900" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s67">
   <Alignment ss:Horizontal="Left" ss:Vertical="Top" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="MetricData">
  <Names>
   <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="=MetricData!R1C1:R1C15"
    ss:Hidden="1"/>
  </Names>
  <Table ss:ExpandedColumnCount="15" ss:ExpandedRowCount="2" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Column ss:AutoFitWidth="0" ss:Width="80.25" ss:Span="2"/>
   <Column ss:Index="4" ss:AutoFitWidth="0" ss:Width="159.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="90" ss:Span="3"/>
   <Column ss:Index="9" ss:AutoFitWidth="0" ss:Width="140.25" ss:Span="1"/>
   <Column ss:Index="11" ss:AutoFitWidth="0" ss:Width="159.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="80.25" ss:Span="2"/>
   <Column ss:Index="15" ss:Width="60.75"/>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s62"><Data ss:Type="String">Type</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">EmpCID</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">SerCID</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">ProviderName</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">ProviderType</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">ServiceArea</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Department</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Specialty</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">UserType</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ReportingPeriodStartDate</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ReportingPeriodEndDate</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s65"><Data ss:Type="String">Metric</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s66"><Data ss:Type="String">Numerator</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s66"><Data ss:Type="String">Denominator</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s66"><Data ss:Type="String">Value</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
   </Row>
   <Row ss:AutoFitHeight="0" ss:Height="60">
    <Cell ss:StyleID="s67"><Data ss:Type="String">Provider</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">123413</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">234123</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Person's Name</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">NURSE PRACTITIONER</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">CHOP SERVICE AREA</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">VIRTUA ORTHOPEDICS</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Orthopedics</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Non-Physician</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">7/28/2019</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">8/31/2019</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Messages Received per Day - Encounter Report</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">0</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">21</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">0</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <Unsynced/>
   <Selected/>
   <FreezePanes/>
   <FrozenNoSplit/>
   <SplitHorizontal>1</SplitHorizontal>
   <TopRowBottomPane>1</TopRowBottomPane>
   <ActivePane>2</ActivePane>
   <Panes>
    <Pane>
     <Number>3</Number>
    </Pane>
    <Pane>
     <Number>2</Number>
     <ActiveCol>3</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
  <AutoFilter x:Range="R1C1:R1C15"
   xmlns="urn:schemas-microsoft-com:office:excel">
  </AutoFilter>
 </Worksheet>
 <x:ExcelWorkbook/>
 <ss:UserWebMetaData>
   <ss:UserFirstName>Stephon</ss:UserFirstName>
   <ss:UserLastName>Proctor</ss:UserLastName>
   <ss:UserFirstEmail>proctors@email.chop.edu</ss:UserFirstEmail>
   <ss:UserID>29c3862c-57c8-487c-8f79-5d50e87af7a7</ss:UserID>
   <ss:Instant>9/24/2019 11:51:52 AM</ss:Instant>
  </ss:UserWebMetaData>
</Workbook>

1 Ответ

0 голосов
/ 25 сентября 2019

Рассмотрим xmlToDataFrame для извлечения ваших конкретных элементов данных, затем запустите миграцию заголовков, начиная с первых 15 извлеченных элементов и, наконец, reshape из длинных в широкие.Поскольку XML поддерживает пространство имен по умолчанию, временный префикс doc назначается для анализа.Ниже предполагается согласованность 15 столбцов не пропущенных Ячейка / Данные узлов.

library(XML)
fileurl <- "pep_slim.xml"

doc <- xmlParse(fileurl)     
nmsp <- c(doc="urn:schemas-microsoft-com:office:spreadsheet")    
df <- xmlToDataFrame(doc, nodes=getNodeSet(doc, "//doc:Cell", nmsp))

df$Headers <- df$Data[1:15]                  # ASSIGN HEADERS TO EVERY 15 ROWS
df <- with(df, df[Data != Headers,])         # SUBSET OUT ORIGINAL HEADERS

# ADD 1-15 ID SEQUENCE (FOR RESHAPING)
df$id <- with(df, ave(as.integer(NamedCell), Headers, FUN=seq_along))

# RESHAPE WIDE TO LONG WITH CLEANUP OF NAMES
rdf <- reshape(df, v.names = "Data", timevar="Headers", idvar="id",
               drop = "NamedCell", direction="wide", sep="_")

colnames(rdf) <- gsub("Data_", "", colnames(rdf))
rownames(rdf) <- NULL

Вывод

rdf
#   id     Type EmpCID SerCID  ProviderName       ProviderType       ServiceArea         Department   Specialty      UserType
# 1  1 Provider 123413 234123 Person's Name NURSE PRACTITIONER CHOP SERVICE AREA VIRTUA ORTHOPEDICS Orthopedics Non-Physician
#   ReportingPeriodStartDate ReportingPeriodEndDate                                       Metric Numerator Denominator Value
# 1                7/28/2019              8/31/2019 Messages Received per Day - Encounter # Report         0          21     0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...