Error: XML content does not seem to be XML: 'NA' for XML data nested in csv file
Error: XML content does not seem to be XML: 'NA' for XML data nested in csv file
我有一个 csv 格式的数据框,在某些单元格中嵌套了 XML 数据(列表)。我想将 XML 列表转换为数据框,然后 select 每个新数据框的两列,然后将这些新数据框绑定在一起。我已经可以在包含数据的单元格上使用 lapply 来完成大部分工作。问题是当我包含包含 <NA>
.
的单元格时,R returns 出错
一些概述:
- 数据框被调用"match"
- 我正在使用的专栏名为 "goal"
- 包含 XML 列表的单元格是 [1729:1733]
- 包含 < NA > 的单元格是 [1727:1728]
- 关于 < NA > 的注意事项:
代码在方括号和 'NA' 之间实际上没有任何空格,但是是的,它确实有方括号,就像 XML 那样。我在这里写了空格,因为当我写的时候没有空格,它就像一个实际的 'NA'。看这里--->)。是的我是菜鸟
以下是我正在使用的库:
library(XML)<br>
library(plyr)<br>
library(dplyr)<br>
这是我成功使用的代码:
var1 <- lapply(match$goal[1729:1733], as.character)
var2 <- lapply(var1, xmlToList)
var3 <- lapply(var2, ldply, data.frame)
var4 <- lapply(var3, subset, select = c("player1", "id"))
如果我尝试包含具有 值的单元格,会发生以下情况:
gar1 <- lapply(match$goal[1727:1733], as.character)
gar2 <- lapply(gar1, xmlToList)
Error: XML content does not seem to be XML: 'NA'
- 如何 运行 此代码以使其跳过 < NA >?
这是数据。抱歉太乱了:
match$goal[1727:1733]
输出(数据样本):
(很抱歉,它作为一个不方便的单行出现。我现在发布它,但我会继续胡思乱想,看看我是否能找到更好的显示方式。
[1] <NA> [2] <NA> [3] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>22</elapsed><player2>38807</player2><subtype>header</subtype><player1>37799</player1><sortorder>5</sortorder><team>10261</team><id>378998</id><n>295</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>24</elapsed><player2>24154</player2><subtype>shot</subtype><player1>24148</player1><sortorder>4</sortorder><team>10260</team><id>379019</id><n>298</n><type>goal</type><goal_type>n</goal_type></value></goal> [4] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>39297</player2><subtype>shot</subtype><player1>26181</player1><sortorder>2</sortorder><team>9825</team><id>375546</id><n>231</n><type>goal</type><goal_type>n</goal_type></value></goal> [5] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>407</event_incident_typefk><elapsed>83</elapsed><player2>30889</player2><subtype>distance</subtype><player1>30853</player1><sortorder>0</sortorder><team>8650</team><id>378041</id><n>344</n><type>goal</type><goal_type>n</goal_type></value></goal> [6] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>36394</player2><subtype>shot</subtype><player1>23139</player1><sortorder>2</sortorder><team>8654</team><id>376060</id><n>244</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>10</elapsed><player2>37277</player2><subtype>shot</subtype><player1>23139</player1><sortorder>1</sortorder><team>8654</team><id>376165</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>411</event_incident_typefk><elapsed>47</elapsed><player2>34466</player2><subtype>volley</subtype><player1>127857</player1><sortorder>3</sortorder><team>8528</team><id>376929</id><n>294</n><type>goal</type><goal_type>n</goal_ty... <truncated> [7] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>47</elapsed><player2>23354</player2><subtype>header</subtype><player1>26165</player1><sortorder>2</sortorder><team>10252</team><id>378837</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>p</comment><stats><penalties>1</penalties></stats><event_incident_typefk>20</event_incident_typefk><elapsed>64</elapsed><player1>40198</player1><sortorder>0</sortorder><team>8456</team><id>378981</id><n>266</n><type>goal</type><goal_type>p</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>69</elapsed><player2>24658</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379030</id><n>270</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</g... <truncated> 13225 Levels: <goal /> .
============================================= ===========================
..
数据,尝试 2(这是从 Excel csv 粘贴的。因此,NA 没有任何括号):
rowID seasonyears match_date_ti matchid goal
1727 2015/2016 9/27/2015 0:00 1979894 NA
1728 2015/2016 9/26/2015 0:00 1979895 NA
1729 2008/2009 8/17/2008 0:00 489042 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>22</elapsed><player2>38807</player2><subtype>header</subtype><player1>37799</player1><sortorder>5</sortorder><team>10261</team><id>378998</id><n>295</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>24</elapsed><player2>24154</player2><subtype>shot</subtype><player1>24148</player1><sortorder>4</sortorder><team>10260</team><id>379019</id><n>298</n><type>goal</type><goal_type>n</goal_type></value></goal>
1730 2008/2009 8/16/2008 0:00 489043 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>39297</player2><subtype>shot</subtype><player1>26181</player1><sortorder>2</sortorder><team>9825</team><id>375546</id><n>231</n><type>goal</type><goal_type>n</goal_type></value></goal>
1731 2008/2009 8/16/2008 0:00 489044 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>407</event_incident_typefk><elapsed>83</elapsed><player2>30889</player2><subtype>distance</subtype><player1>30853</player1><sortorder>0</sortorder><team>8650</team><id>378041</id><n>344</n><type>goal</type><goal_type>n</goal_type></value></goal>
1732 2008/2009 8/16/2008 0:00 489045 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>36394</player2><subtype>shot</subtype><player1>23139</player1><sortorder>2</sortorder><team>8654</team><id>376060</id><n>244</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>10</elapsed><player2>37277</player2><subtype>shot</subtype><player1>23139</player1><sortorder>1</sortorder><team>8654</team><id>376165</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>411</event_incident_typefk><elapsed>47</elapsed><player2>34466</player2><subtype>volley</subtype><player1>127857</player1><sortorder>3</sortorder><team>8528</team><id>376929</id><n>294</n><type>goal</type><goal_type>n</goal_type></value></goal>
1733 2008/2009 8/17/2008 0:00 489046 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>47</elapsed><player2>23354</player2><subtype>header</subtype><player1>26165</player1><sortorder>2</sortorder><team>10252</team><id>378837</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>p</comment><stats><penalties>1</penalties></stats><event_incident_typefk>20</event_incident_typefk><elapsed>64</elapsed><player1>40198</player1><sortorder>0</sortorder><team>8456</team><id>378981</id><n>266</n><type>goal</type><goal_type>p</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>69</elapsed><player2>24658</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379030</id><n>270</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>74</elapsed><player2>23782</player2><subtype>header</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379074</id><n>283</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>76</elapsed><player2>23782</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379095</id><n>277</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>90</elapsed><player2>45431</player2><subtype>shot</subtype><player1>46403</player1><sortorder>1</sortorder><team>8456</team><id>379250</id><n>281</n><type>goal</type><goal_type>n</goal_type></value></goal>
考虑为非 NA 值设置子集或在 lapply 调用中使用 tryCatch
:
数据
library(XML)
library(plyr)
library(dplyr)
txt = 'rowID season_years match_date_ti matchid goal
1727 "2015/2016" "9/27/2015 0:00" 1979894 "NA"
1728 "2015/2016" "9/26/2015 0:00" 1979895 "NA"
1729 "2008/2009" "8/17/2008 0:00" 489042 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>22</elapsed><player2>38807</player2><subtype>header</subtype><player1>37799</player1><sortorder>5</sortorder><team>10261</team><id>378998</id><n>295</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>24</elapsed><player2>24154</player2><subtype>shot</subtype><player1>24148</player1><sortorder>4</sortorder><team>10260</team><id>379019</id><n>298</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1730 "2008/2009" "8/16/2008 0:00" 489043 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>39297</player2><subtype>shot</subtype><player1>26181</player1><sortorder>2</sortorder><team>9825</team><id>375546</id><n>231</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1731 "2008/2009" "8/16/2008 0:00" 489044 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>407</event_incident_typefk><elapsed>83</elapsed><player2>30889</player2><subtype>distance</subtype><player1>30853</player1><sortorder>0</sortorder><team>8650</team><id>378041</id><n>344</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1732 "2008/2009" "8/16/2008 0:00" 489045 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>36394</player2><subtype>shot</subtype><player1>23139</player1><sortorder>2</sortorder><team>8654</team><id>376060</id><n>244</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>10</elapsed><player2>37277</player2><subtype>shot</subtype><player1>23139</player1><sortorder>1</sortorder><team>8654</team><id>376165</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>411</event_incident_typefk><elapsed>47</elapsed><player2>34466</player2><subtype>volley</subtype><player1>127857</player1><sortorder>3</sortorder><team>8528</team><id>376929</id><n>294</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1733 "2008/2009" "8/17/2008 0:00" 489046 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>47</elapsed><player2>23354</player2><subtype>header</subtype><player1>26165</player1><sortorder>2</sortorder><team>10252</team><id>378837</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>p</comment><stats><penalties>1</penalties></stats><event_incident_typefk>20</event_incident_typefk><elapsed>64</elapsed><player1>40198</player1><sortorder>0</sortorder><team>8456</team><id>378981</id><n>266</n><type>goal</type><goal_type>p</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>69</elapsed><player2>24658</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379030</id><n>270</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>74</elapsed><player2>23782</player2><subtype>header</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379074</id><n>283</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>76</elapsed><player2>23782</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379095</id><n>277</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>90</elapsed><player2>45431</player2><subtype>shot</subtype><player1>46403</player1><sortorder>1</sortorder><team>8456</team><id>379250</id><n>281</n><type>goal</type><goal_type>n</goal_type></value></goal>"'
match <- read.table(text=txt, sep="", header=TRUE, na.strings = "NA")
非 NA 子集方法
var1 <- lapply(match[!is.na(match$goal), c("goal")], as.character)
var2 <- lapply(var1, xmlToList)
var3 <- lapply(var2, ldply, data.frame)
var4 <- lapply(var3, subset, select = c("player1", "id"))
TryCatch 方法 (return NA 值的空 <goal></goal>
)
var2 <- lapply(match$goal, function(i) {
tryCatch({ return(xmlToList(as.character(i)))
}, error = function(e) return(xmlToList('<goal></goal>')))
})
var3 <- Filter(function(i) nrow(i) > 1, lapply(var2, ldply, data.frame))
var4 <- lapply(var3, subset, select = c("player1", "id"))
If/Else方法
var2 <- lapply(match$goal, function(i) {
if(!is.na(i))
return(xmlToList(as.character(i)))
else
return(xmlToList('<goal></goal>'))
})
var3 <- Filter(function(i) nrow(i) > 1, lapply(var2, ldply, data.frame))
var4 <- lapply(var3, subset, select = c("player1", "id"))
我有一个 csv 格式的数据框,在某些单元格中嵌套了 XML 数据(列表)。我想将 XML 列表转换为数据框,然后 select 每个新数据框的两列,然后将这些新数据框绑定在一起。我已经可以在包含数据的单元格上使用 lapply 来完成大部分工作。问题是当我包含包含 <NA>
.
一些概述:
- 数据框被调用"match"
- 我正在使用的专栏名为 "goal"
- 包含 XML 列表的单元格是 [1729:1733]
- 包含 < NA > 的单元格是 [1727:1728]
- 关于 < NA > 的注意事项:
代码在方括号和 'NA' 之间实际上没有任何空格,但是是的,它确实有方括号,就像 XML 那样。我在这里写了空格,因为当我写的时候没有空格,它就像一个实际的 'NA'。看这里--->)。是的我是菜鸟
以下是我正在使用的库:
library(XML)<br>
library(plyr)<br>
library(dplyr)<br>
这是我成功使用的代码:
var1 <- lapply(match$goal[1729:1733], as.character)
var2 <- lapply(var1, xmlToList)
var3 <- lapply(var2, ldply, data.frame)
var4 <- lapply(var3, subset, select = c("player1", "id"))
如果我尝试包含具有
gar1 <- lapply(match$goal[1727:1733], as.character)
gar2 <- lapply(gar1, xmlToList)
Error: XML content does not seem to be XML: 'NA'
- 如何 运行 此代码以使其跳过 < NA >?
这是数据。抱歉太乱了:
match$goal[1727:1733]
输出(数据样本):
(很抱歉,它作为一个不方便的单行出现。我现在发布它,但我会继续胡思乱想,看看我是否能找到更好的显示方式。
[1] <NA> [2] <NA> [3] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>22</elapsed><player2>38807</player2><subtype>header</subtype><player1>37799</player1><sortorder>5</sortorder><team>10261</team><id>378998</id><n>295</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>24</elapsed><player2>24154</player2><subtype>shot</subtype><player1>24148</player1><sortorder>4</sortorder><team>10260</team><id>379019</id><n>298</n><type>goal</type><goal_type>n</goal_type></value></goal> [4] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>39297</player2><subtype>shot</subtype><player1>26181</player1><sortorder>2</sortorder><team>9825</team><id>375546</id><n>231</n><type>goal</type><goal_type>n</goal_type></value></goal> [5] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>407</event_incident_typefk><elapsed>83</elapsed><player2>30889</player2><subtype>distance</subtype><player1>30853</player1><sortorder>0</sortorder><team>8650</team><id>378041</id><n>344</n><type>goal</type><goal_type>n</goal_type></value></goal> [6] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>36394</player2><subtype>shot</subtype><player1>23139</player1><sortorder>2</sortorder><team>8654</team><id>376060</id><n>244</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>10</elapsed><player2>37277</player2><subtype>shot</subtype><player1>23139</player1><sortorder>1</sortorder><team>8654</team><id>376165</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>411</event_incident_typefk><elapsed>47</elapsed><player2>34466</player2><subtype>volley</subtype><player1>127857</player1><sortorder>3</sortorder><team>8528</team><id>376929</id><n>294</n><type>goal</type><goal_type>n</goal_ty... <truncated> [7] <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>47</elapsed><player2>23354</player2><subtype>header</subtype><player1>26165</player1><sortorder>2</sortorder><team>10252</team><id>378837</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>p</comment><stats><penalties>1</penalties></stats><event_incident_typefk>20</event_incident_typefk><elapsed>64</elapsed><player1>40198</player1><sortorder>0</sortorder><team>8456</team><id>378981</id><n>266</n><type>goal</type><goal_type>p</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>69</elapsed><player2>24658</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379030</id><n>270</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</g... <truncated> 13225 Levels: <goal /> .
============================================= ===========================
..
数据,尝试 2(这是从 Excel csv 粘贴的。因此,NA 没有任何括号):
rowID seasonyears match_date_ti matchid goal
1727 2015/2016 9/27/2015 0:00 1979894 NA
1728 2015/2016 9/26/2015 0:00 1979895 NA
1729 2008/2009 8/17/2008 0:00 489042 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>22</elapsed><player2>38807</player2><subtype>header</subtype><player1>37799</player1><sortorder>5</sortorder><team>10261</team><id>378998</id><n>295</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>24</elapsed><player2>24154</player2><subtype>shot</subtype><player1>24148</player1><sortorder>4</sortorder><team>10260</team><id>379019</id><n>298</n><type>goal</type><goal_type>n</goal_type></value></goal>
1730 2008/2009 8/16/2008 0:00 489043 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>39297</player2><subtype>shot</subtype><player1>26181</player1><sortorder>2</sortorder><team>9825</team><id>375546</id><n>231</n><type>goal</type><goal_type>n</goal_type></value></goal>
1731 2008/2009 8/16/2008 0:00 489044 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>407</event_incident_typefk><elapsed>83</elapsed><player2>30889</player2><subtype>distance</subtype><player1>30853</player1><sortorder>0</sortorder><team>8650</team><id>378041</id><n>344</n><type>goal</type><goal_type>n</goal_type></value></goal>
1732 2008/2009 8/16/2008 0:00 489045 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>36394</player2><subtype>shot</subtype><player1>23139</player1><sortorder>2</sortorder><team>8654</team><id>376060</id><n>244</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>10</elapsed><player2>37277</player2><subtype>shot</subtype><player1>23139</player1><sortorder>1</sortorder><team>8654</team><id>376165</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>411</event_incident_typefk><elapsed>47</elapsed><player2>34466</player2><subtype>volley</subtype><player1>127857</player1><sortorder>3</sortorder><team>8528</team><id>376929</id><n>294</n><type>goal</type><goal_type>n</goal_type></value></goal>
1733 2008/2009 8/17/2008 0:00 489046 <goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>47</elapsed><player2>23354</player2><subtype>header</subtype><player1>26165</player1><sortorder>2</sortorder><team>10252</team><id>378837</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>p</comment><stats><penalties>1</penalties></stats><event_incident_typefk>20</event_incident_typefk><elapsed>64</elapsed><player1>40198</player1><sortorder>0</sortorder><team>8456</team><id>378981</id><n>266</n><type>goal</type><goal_type>p</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>69</elapsed><player2>24658</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379030</id><n>270</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>74</elapsed><player2>23782</player2><subtype>header</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379074</id><n>283</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>76</elapsed><player2>23782</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379095</id><n>277</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>90</elapsed><player2>45431</player2><subtype>shot</subtype><player1>46403</player1><sortorder>1</sortorder><team>8456</team><id>379250</id><n>281</n><type>goal</type><goal_type>n</goal_type></value></goal>
考虑为非 NA 值设置子集或在 lapply 调用中使用 tryCatch
:
数据
library(XML)
library(plyr)
library(dplyr)
txt = 'rowID season_years match_date_ti matchid goal
1727 "2015/2016" "9/27/2015 0:00" 1979894 "NA"
1728 "2015/2016" "9/26/2015 0:00" 1979895 "NA"
1729 "2008/2009" "8/17/2008 0:00" 489042 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>22</elapsed><player2>38807</player2><subtype>header</subtype><player1>37799</player1><sortorder>5</sortorder><team>10261</team><id>378998</id><n>295</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>24</elapsed><player2>24154</player2><subtype>shot</subtype><player1>24148</player1><sortorder>4</sortorder><team>10260</team><id>379019</id><n>298</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1730 "2008/2009" "8/16/2008 0:00" 489043 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>39297</player2><subtype>shot</subtype><player1>26181</player1><sortorder>2</sortorder><team>9825</team><id>375546</id><n>231</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1731 "2008/2009" "8/16/2008 0:00" 489044 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>407</event_incident_typefk><elapsed>83</elapsed><player2>30889</player2><subtype>distance</subtype><player1>30853</player1><sortorder>0</sortorder><team>8650</team><id>378041</id><n>344</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1732 "2008/2009" "8/16/2008 0:00" 489045 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>4</elapsed><player2>36394</player2><subtype>shot</subtype><player1>23139</player1><sortorder>2</sortorder><team>8654</team><id>376060</id><n>244</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>10</elapsed><player2>37277</player2><subtype>shot</subtype><player1>23139</player1><sortorder>1</sortorder><team>8654</team><id>376165</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>411</event_incident_typefk><elapsed>47</elapsed><player2>34466</player2><subtype>volley</subtype><player1>127857</player1><sortorder>3</sortorder><team>8528</team><id>376929</id><n>294</n><type>goal</type><goal_type>n</goal_type></value></goal>"
1733 "2008/2009" "8/17/2008 0:00" 489046 "<goal><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>47</elapsed><player2>23354</player2><subtype>header</subtype><player1>26165</player1><sortorder>2</sortorder><team>10252</team><id>378837</id><n>251</n><type>goal</type><goal_type>n</goal_type></value><value><comment>p</comment><stats><penalties>1</penalties></stats><event_incident_typefk>20</event_incident_typefk><elapsed>64</elapsed><player1>40198</player1><sortorder>0</sortorder><team>8456</team><id>378981</id><n>266</n><type>goal</type><goal_type>p</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>69</elapsed><player2>24658</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379030</id><n>270</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>406</event_incident_typefk><elapsed>74</elapsed><player2>23782</player2><subtype>header</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379074</id><n>283</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>76</elapsed><player2>23782</player2><subtype>shot</subtype><player1>23264</player1><sortorder>1</sortorder><team>10252</team><id>379095</id><n>277</n><type>goal</type><goal_type>n</goal_type></value><value><comment>n</comment><stats><goals>1</goals><shoton>1</shoton></stats><event_incident_typefk>393</event_incident_typefk><elapsed>90</elapsed><player2>45431</player2><subtype>shot</subtype><player1>46403</player1><sortorder>1</sortorder><team>8456</team><id>379250</id><n>281</n><type>goal</type><goal_type>n</goal_type></value></goal>"'
match <- read.table(text=txt, sep="", header=TRUE, na.strings = "NA")
非 NA 子集方法
var1 <- lapply(match[!is.na(match$goal), c("goal")], as.character)
var2 <- lapply(var1, xmlToList)
var3 <- lapply(var2, ldply, data.frame)
var4 <- lapply(var3, subset, select = c("player1", "id"))
TryCatch 方法 (return NA 值的空 <goal></goal>
)
var2 <- lapply(match$goal, function(i) {
tryCatch({ return(xmlToList(as.character(i)))
}, error = function(e) return(xmlToList('<goal></goal>')))
})
var3 <- Filter(function(i) nrow(i) > 1, lapply(var2, ldply, data.frame))
var4 <- lapply(var3, subset, select = c("player1", "id"))
If/Else方法
var2 <- lapply(match$goal, function(i) {
if(!is.na(i))
return(xmlToList(as.character(i)))
else
return(xmlToList('<goal></goal>'))
})
var3 <- Filter(function(i) nrow(i) > 1, lapply(var2, ldply, data.frame))
var4 <- lapply(var3, subset, select = c("player1", "id"))