你如何检查R中是否存在列表

时间:2015-07-04 02:11:01

标签: r

我正在阅读带有rvest包的html表:

这是我试图阅读的html文件。在这种情况下,q [[2]]存在,但有时,q [[2]]不存在:

<html> <head> <meta http-equiv="Refresh" content="60, cecutil2.cgi?V1P1"/> <style type="text/css"> th,td {font-size:12px; height:14px} table {border-collapse:collapse;} </style> </head> <body> <table width="100%" cols="6" border="2px">
<tr><td style="width:50px; text-align:center"><a href="zview.html" target="_blank" title="v1p1.mf.example.com:1024 (10.175.128.62)">V1P1</a></td><th width="55px">15/06/22</th><th width="40px">12:21</th>
<th width="65px">3B0F97-0</th><th style="width:50px">19.11%</th> <td align="left"><img height="100%" src="grnline.gif" width="19.10954%"></td></tr>
<tr><td colspan="3"></td>
<th width="65px">3B0F97-1</th><th style="width:50px">20.69%</th> <td align="left"><img height="100%" src="grnline.gif" width="20.68669%"></td></tr>
<tr><td colspan="3"></td>
<th width="65px">3B0F97-2</th><th style="width:50px">22.14%</th> <td align="left"><img height="100%" src="grnline.gif" width="22.13768%"></td></tr>
<tr><td colspan="3"></td>
<th width="65px">3B0F97-3</th><th style="width:50px">35.25%</th> <td align="left"><img height="100%" src="grnline.gif" width="35.24557%"></td></tr>
<tr><td colspan="3"></td>
<th width="65px">3B0F97-4</th><th style="width:50px">38.04%</th> <td align="left"><img height="100%" src="grnline.gif" width="38.03592%"></td></tr>
<tr><td colspan="3"></td>
<th width="65px">3B0F97-5</th><th style="width:50px">23.73%</th> <td align="left"><img height="100%" src="grnline.gif" width="23.73468%"></td></tr>
<tr><td colspan="3"></td>
<th width="65px"> Total </th><th style="width:50px">659.55%</th> <td align="left"><img height="100%" src="grnline.gif" width="32.977437%"></td></tr>
<tr><th colspan="6" align="center">Linux Nodes (z/VM-Guests)</th></tr> <tr><td/><td colspan="5"><table cols="3" width="100%" border="1px">
<tr><th width="50px"> <a href="http://jassadmin.nj.example.com/adminsvcs/jassgleprocess.jsp?search_criteria=jas1a419" target="_blank">jas1a419</a></th><th style="width:50px">121.58%</th> <td align="left"><img height="100%" src="redline.png" width="100%"></td></tr>
<tr><th width="50px"> <a href="http://jassadmin.nj.example.com/adminsvcs/jassgleprocess.jsp?search_criteria=jas1a443" target="_blank">jas1a443</a></th><th style="width:50px">45.07%</th> <td align="left"><img height="100%" src="grnline.gif" width="45.07199%"></td></tr>
<tr><th width="50px"> <a href="http://jassadmin.nj.example.com/adminsvcs/jassgleprocess.jsp?search_criteria=jas1a185" target="_blank">jas1a185</a></th><th style="width:50px">36.53%</th> <td align="left"><img height="100%" src="grnline.gif" width="36.52853%"></td></tr>
<tr><th width="50px"> <a href="http://jassadmin.nj.example.com/adminsvcs/jassgleprocess.jsp?search_criteria=jas1a435" target="_blank">jas1a435</a></th><th style="width:50px">23.19%</th> <td align="left"><img height="100%" src="grnline.gif" width="23.18803%"></td></tr>
</td></tr></table>
</table></body></html>


library(rvest)
q <- html(url1) %>% html_table(fill=T)
t2 <- as.data.frame(q[[2]][-3])
t1 <- as.data.frame(q[[1]])

它有效,但有时q没有第二个系列只有q [[1]]而且我的脚本失败了。有没有办法检查q [[2]]是否存在然后进行分配?

1 个答案:

答案 0 :(得分:3)

这是我的解决方案,只是 $scope.allTastes.then(function(tastes){ $scope.tags = [tastes[0],tastes[1]] $scope.allTags = tastes; // create a new $scope to get all tags from the promise }) /** * Search for contacts. */ function querySearch (query) { console.log($scope.allTastes) // return a promise $scope.allTastes= $scope.allTags; // pass it to $scope.allTastes, now it has all the array var results = query ? $scope.allTastes.filter(createFilterFor(query)) : []; return results; } 的快捷方式。该函数的行为是:

  

apply返回与X相同长度的列表,其中每个元素都是   将FUN应用于X的相应元素的结果。

lapply

编辑评论:

正如BlondedDust所指出的那样:

li <- list(NA, "string", 10, TRUE, NaN)
 lapply(li, is.na)
[[1]]
[1] TRUE

[[2]]
[1] FALSE

[[3]]
[1] FALSE

[[4]]
[1] FALSE

[[5]]
[1] TRUE