rbind列表中的所有给定列

时间:2015-07-22 09:03:55

标签: r list rbind read.csv

我正在读取当前工作目录中包含的可变数量的.csv文件到列表中,并且想要查找每个.csv文件的第二列。

工作目录中的文件如下所示:

150601_0001.csv
150601_0002.csv
150601_0003.csv
etc.

我有以下代码将它们全部读入目录中任何给定数量的文件的列表中: (代码来自here

myfiles <- dir(pattern = "\\.(csv|CSV)$", full.names = TRUE) # get filenames and paths
myfiles_data <- lapply(myfiles, data.table::fread) # read data from files, quickly
head(myfiles_data[[1]]) # validate file reading
names(myfiles_data) <- myfiles # assign names to list items

到目前为止,这种方法完全正常,我将所有数据都列入了一个漂亮的列表:

> myfiles_data
$`./150601_0001.csv`
       X     Y Z
  1:   1 67.81 1
  2:   2 68.52 1
  3:   3 69.66 1
 ---            
250: 250 50.02 1
251: 251 50.58 1
252: 252 51.16 1

$`./150601_0002.csv`
       X     Y Z
  1:   1 70.77 2
  2:   2 70.54 2
  3:   3 70.47 2
 ---            
250: 250 51.00 2
251: 251 51.17 2
252: 252 51.43 2

$`./150601_0003.csv`
       X     Y Z
  1:   1 68.32 3
  2:   2 67.80 3
  3:   3 67.33 3
 ---            
250: 250 50.58 3
251: 251 50.68 3
252: 252 50.77 3

现在我想在每个数据集的第二列上进行rbind。下面的代码给了我一个只有第二列的列表(数据是为了视觉目的而缩写):

> lapply(myfiles_data, `[[`, 2)
$`./150601_0001.csv`
  [1] 67.81 68.52 69.66 ...
  ...
[241] ... 52.85 51.85 50.90

$`./150601_0002.csv`
  [1] 70.77  70.54  70.47 ...
  ...
[241] ... 51.00  51.17  51.43

$`./150601_0003.csv`
  [1] 68.32 67.80 67.33 ...
  ...
[241] ... 50.58 50.68 50.77

如何一次性将rbind()应用于所有这些?

2 个答案:

答案 0 :(得分:1)

没有声誉可以添加评论,因此添加答案

尝试将所有数据转换为一个数据帧

Output<-do.call(rbind, list)

答案 1 :(得分:0)

怎么样

var shipSearchResultTable;
function dataSetup(domain){
	
	if ( $.fn.dataTable.isDataTable( '#shipSearchResultTable' ) ) {
	    shipSearchResultTable = $('#shipSearchResultTable').DataTable();
	}
	else {
		if(domain=='GTOCE'){
		    shipSearchResultTable = $('#shipSearchResultTable').DataTable( {
		        "sPaginationType" : "full_numbers",
		        "scrollY": 350,
		        "bJQueryUI" : true,
				"scrollX": true,
				"sDom": '<"H"lfr>t<"F"ip>T',
				"oTableTools": {
		            "sSwfPath": "resources/jquery/desktop/js/copy_csv_xls_pdf.swf",
		            "aButtons": [
		                         "copy",
		                         "csv",
		                         {
		                             "sExtends": "pdf",
		                             "sPdfOrientation": "landscape",
		                             "sPdfSize": "A3"
		                         },
		                         "print"
		                     ]
		        },
		        "language": {
					"emptyTable": "No shipment found"
				},
				"order" : [[ 10, "asc" ]],
				"aoColumnDefs": [{ "sType": "date", "aTargets": [10] }],
				"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
		            $(nRow).click( function() {
		            	
		        });
		    } 
		    } );
		}else{
			shipSearchResultTable = $('#shipSearchResultTable').DataTable( {
		        "sPaginationType" : "full_numbers",
		        "scrollY": 350,
		        "bJQueryUI" : true,
				"scrollX": true,
				"sDom": '<"H"lfr>t<"F"ip>T',
				"oTableTools": {
		            "sSwfPath": "resources/jquery/desktop/js/copy_csv_xls_pdf.swf",
		            "aButtons": [
		                         "copy",
		                         "csv",
		                         {
		                             "sExtends": "pdf",
		                             "sPdfOrientation": "landscape",
		                             "sPdfSize": "A3"
		                         },
		                         "print"
		                     ]
		        },
		        "language": {
					"emptyTable": "No shipment found"
				},
				"order" : [[ 4, "asc" ]],
				"aoColumnDefs": [{ "sType": "date", "aTargets": [4] }],
				"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
		            $(nRow).click( function() {
		            	
		        });
		    } 
		    } );
		}
	}

	$('#shipSearchResultTable tbody').on( 'click', 'tr', function () {
		if(shipSearchResultTable.context[0].aoData.length > 0){    
			if (! $(this).hasClass('selected') ) {
		        	shipSearchResultTable.$('tr.selected').removeClass('selected');
		            $(this).addClass('selected');
		        }
			showSpinnerWheel();
			if(domain=='GTOCE'){
		        loadShipDetailPopup($('td', this).eq(0).text(), $('td', this).eq(11).text());
			}else{
				loadShipDetailPopup($('td', this).eq(0).text(), $('td', this).eq(5).text());
			}
	        }
	    } );

	$( "#date_from,#date_to" ).datepicker({
				showOn: "button",
				buttonText: "date picker",
				prevText: "",
				nextText: "",
				buttonImage: "resources/jquery/desktop/images/calendar.gif",
				dateFormat: "yy-mm-dd",
				buttonImageOnly: true,
				defaultDate: new Date()
			});
	$('#submitBtn').on( 'click', function () {
		showSpinnerWheel();
		getSearchResultData(domain);
	} );
} 
 
function loadShipDetailPopup(shipmentNumber, loadNumber){
	$("#shipDetPopup").dialog({
		modal: true,
		autoOpen: false,
	    autoResize: true,
	    hide: "fold",
	    title:'Shipment Details',
	    position: 'center' ,
	    maxHeight: 1000,
	    open: function(event, ui) { $(this).css({"max-height": 1000, "overflow-y": "auto"}); },
	    resizable: true,
	    show: "fadeIn",
	    position: { my: "center top", at:"center top", of: window },
	    width: 1000
	});
	 $.ajax({
         type: "GET",
         url: "shipment_detail.st",
         data: {shpm_num:shipmentNumber, ld_leg_id: loadNumber}
   }).done(function(data) {
	   $("#shipDetPopup").load($(data)).dialog("open");
	   $("#shipDetPopupContent").html(data);
	   hideSpinnerWheel();
   }); 
}

function getSearchResultData(domain){
	var id = "ship_search_form";
	 shipSearchResultTable = $('#shipSearchResultTable').dataTable();
	var values = $("#"+id).serialize();
	$.ajax( {
		type: "GET",
        url: "search_list_desktop.st",
        data: values,
		success : function(response) {
		
			if (response.status == "SUCCESS") {
				var resultLength = response.result.length;
				shipSearchResultTable.fnClearTable();
				if(domain == "GTOCE"){
				for ( var i = 0; i < resultLength; i++) {
					var shipSearchResultData = [];
					
					shipSearchResultData.push((response.result[i].number));	
					if(response.result[i].loadStatus == 5220){
						shipSearchResultData.push("<img width='20' height='20' src='resources/images/icon-shp-status-"+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.short']+".png' /> "+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.long']);
					}else if(response.result[i].loadStatus == 5210){
						shipSearchResultData.push("<img width='20' height='20'	src='resources/images/icon-shp-status-"+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.short']+".png' /> "+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.long']);
					}else{
						shipSearchResultData.push("<img width='20' height='20'	src='resources/images/icon-shp-status-"+statusKeyMap['ship.status.'+response.result[i].statusCode+'.short']+".png' /> "+statusKeyMap['ship.status.'+response.result[i].statusCode+'.long']);
					}
					shipSearchResultData.push((response.result[i].originLocation));
					shipSearchResultData.push((response.result[i].originLocationName));
					shipSearchResultData.push((response.result[i].orgCityName));
					shipSearchResultData.push((response.result[i].orgCntryCd));
					shipSearchResultData.push((response.result[i].destinationLocation));
					shipSearchResultData.push((response.result[i].destinationLocationName));
					shipSearchResultData.push((response.result[i].destCityName));
					shipSearchResultData.push((response.result[i].destCntryCd));
					shipSearchResultData.push(response.result[i].expectedDeliveryDate);
					shipSearchResultData.push((response.result[i].loadNumber));
					shipSearchResultData.push((response.result[i].loadTrackingNo));
					shipSearchResultData.push((response.result[i].carrierName));
					shipSearchResultData.push((response.result[i].trailerNumber));
					shipSearchResultData.push((response.result[i].equpimentDesc));
					shipSearchResultData.push((response.result[i].serviceType));
					shipSearchResultData.push((response.result[i].orderType));
					shipSearchResultData.push((response.result[i].consigneeLocation));
					shipSearchResultData.push((response.result[i].authorizationNumber));
					shipSearchResultData.push((response.result[i].toReferenceNumber));
					
					var row = shipSearchResultTable.fnAddData(shipSearchResultData);
					var nTr = shipSearchResultTable.fnSettings().aoData[ row[0] ].nTr;
					nTr.style.color = response.result[i].priority;
					
				}
			}else{
				for ( var i = 0; i < resultLength; i++) {
					var shipSearchResultData = [];
					
					shipSearchResultData.push((response.result[i].number));	
					if(response.result[i].loadStatus == 5220){
						shipSearchResultData.push("<img width='20' height='20' src='resources/images/icon-shp-status-"+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.short']+".png' /> "+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.long']);
					}else if(response.result[i].loadStatus == 5210){
						shipSearchResultData.push("<img width='20' height='20'	src='resources/images/icon-shp-status-"+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.short']+".png' /> "+statusKeyMap['ship.load.status.'+response.result[i].loadStatus+'.long']);
					}else{
						shipSearchResultData.push("<img width='20' height='20'	src='resources/images/icon-shp-status-"+statusKeyMap['ship.status.'+response.result[i].statusCode+'.short']+".png' /> "+statusKeyMap['ship.status.'+response.result[i].statusCode+'.long']);
					}
					shipSearchResultData.push((response.result[i].originLocation)+", "+(response.result[i].orgCityName)+" ,"+(response.result[i].orgStateCd));
					shipSearchResultData.push((response.result[i].destinationLocation)+", "+(response.result[i].destCityName)+" ,"+(response.result[i].destStateCd));
					
					shipSearchResultData.push(response.result[i].expectedDeliveryDate);
					shipSearchResultData.push((response.result[i].loadNumber));
					shipSearchResultData.push((response.result[i].loadTrackingNo));
					shipSearchResultData.push((response.result[i].carrierName));
					shipSearchResultData.push((response.result[i].trailerNumber));
					shipSearchResultData.push((response.result[i].equpimentDesc));
										
					var row = shipSearchResultTable.fnAddData(shipSearchResultData);
					var nTr = shipSearchResultTable.fnSettings().aoData[ row[0] ].nTr;
					nTr.style.color = response.result[i].priority;
					
				}
			}

			} else {
				document.getElementById("jpt_err").style.display = 'block';
			}
		},
		error : function(e) {
			document.getElementById("jpt_err").style.display = 'block';
		}
	}

	);
}