删除具有3个或更多NA值的行 - R.

时间:2016-09-09 13:14:06

标签: r

我想删除所有包含3个或更多NA值的行 - 下面是我的数据集的一个示例(但实际数据集有95,000行)

Plot_ID  Tree_ID   Dbh13   Dbh08  Dbh03  Dbh93_94
  106       6         236     132    123     132    
  495       8         134     NA     NA      102
  984       12        NA      123    110     97

所以它看起来像这个

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    	<title>Test</title>
    	<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.js"></script>
    	<script type="text/javascript">
    	var checkedRows = [];
    	$('#eventsTable').on('.bs-checkbox', function (e, row) {
    		checkedRows.push({id: row.id, name: row.name, forks: row.forks});
    	  console.log(checkedRows);
    	});
    
    	$('#eventsTable').on('uncheck.bs.table', function (e, row) {
    	  $.each(checkedRows, function(index, value) {
    	    if (value.id === row.id) {
    	      checkedRows.splice(index,1);
    	    }
    	  });
    	  console.log(checkedRows);
    	});
    
    	$("#add_cart").click(function() {
    	  $("#output").empty();
    	  console.log(checkedRows);
    	  $.each(checkedRows, function(index, value) {
    	    $('#output').append($('<li></li>').text(value.id + " | " + value.name + " | " + value.forks));
    	  });
    	});
    	</script>
    </head>
    
    <body>
    	<div style="position: absolute;bottom: 42px; padding-left:10px;  "> 
    	 <table id="eventsTable"
           data-toggle="table"
           data-height="300"
           data-url="https://api.github.com/users/wenzhixin/repos?type=owner&sort=full_name&direction=asc&per_page=100&page=1"
           data-pagination="true"
           data-search="true"
           data-show-refresh="true"
           data-show-toggle="true"
           data-show-columns="true"
           data-toolbar="#toolbar">
        <thead>
        <tr>
            <th data-field="state" data-checkbox="true"></th>
            <th data-field="name">Name</th>
            <th data-field="stargazers_count">Stars</th>
            <th data-field="forks_count">Forks</th>
            <th data-field="description">Description</th>
        </tr>
        </thead>
    </table>
    
    <button id="add_cart">Add to card</button>
    <ul id="output"></ul>
    </div>
    </body>
    </html>

1 个答案:

答案 0 :(得分:2)

Plot_ID  Tree_ID   Dbh13   Dbh08  Dbh03  Dbh93_94
106       6         236     132    123     132    
204       5         NA      NA     NA      142
495       8         134     NA     NA      102
984       12        NA      123    110     97

df1    <- read.table(con<-file("clipboard"),header=T)

cnt_na <- apply(df1, 1, function(z) sum(is.na(z)))

df1[cnt_na < 3,]
  Plot_ID Tree_ID Dbh13 Dbh08 Dbh03 Dbh93_94
1     106       6   236   132   123      132
3     495       8   134    NA    NA      102
4     984      12    NA   123   110       97