2012-06-27 56 views
1

我目前正在使用2個數據框,其中一個我簡單地稱爲Data,另一個稱爲DataOutput。數據對21個變量的觀察結果超過40萬,並且DataOutput只有4個觀察值的21個變量。 DataOutput是一個數據幀,包括簡單NA和OOR(OutofRange)和#Measurements和Ratio((NA + OOR)/#Measurements)的不同總和。數據數據框當前包含很多隻包含NA的列,因爲那裏只是沒有對這些變量進行測量。如何根據來自另一個數據框的條件從數據框中刪除

我想擺脫其中只有NA的列。

for(z in 2:22) 
{ 

if(DataOutput[4,z] == 1) //This is the ratio ((NA+OOR)/#Measurements) == 1 
{ 
    Data <- subset(Data, select = -Data[,z]) 
} 
} 

我試圖這樣做,但它不起作用。我不能簡單地對我需要刪除的列進行硬編碼,如select = - c(a,b)或其他東西,因爲代碼必須處理許多不同大小的數據幀。他們都有不同的列,完全不可用。

任何想法?幫助真的很感激!

> dput(head(Data)) 
structure(list(StartTime = structure(c(1169218200, 1169218800, 
1169219400, 1169220000, 1169220600, 1169221200), class = c("POSIXct", 
"POSIXt"), tzone = ""), Latitude = c(15.6383658333333, 15.648397, 
15.6581663333333, 15.6680338333333, 15.6778031666667, 15.6876706666667 
), Longitude = c(15.8445643333333, 15.8549853333333, 15.8651343333333, 
15.8753853333333, 15.8855343333333, 15.8957853333333), GPSSpeed = c(NA, 
NA, 315, 315, 315, 315), LogSpeed = c(NA, NA, 696.091532743333, 
697.291378813333, 698.491512383334, 699.691533736667), WindSpeedRel = c(NA, 
NA, 1.03611152968314, 1.00016348803882, 1.06045149695061, 0.995509934806929 
), WindDirRel = c(NA, NA, 1.38425886694239, 1.29982376776468, 
1.37160349066357, 1.33137136705896), Course = c(NA, NA, NA, NA, 
NA, NA), SeaDepth = c(NA, NA, NA, NA, NA, NA), DraftFWD = c(NA, 
NA, NA, NA, NA, NA), DraftAFT = c(NA, NA, NA, NA, NA, NA), Rudder = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), PropellerKW = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MEConsumption = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MERPM = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MELoad = c(NA, 
NA, NA, NA, NA, NA), DGConsumption = c(NA, NA, NA, NA, NA, NA 
), DG1_Load = c(NA, NA, NA, NA, NA, NA), DG2_Load = c(NA, NA, 
NA, NA, NA, NA), DG3_Load = c(NA, NA, NA, NA, NA, NA), DG4_Load = c(NA, 
NA, NA, NA, NA, NA), DG5_Load = c(NA, NA, NA, NA, NA, NA)), .Names = c("StartTime", 
"Latitude", "Longitude", "GPSSpeed", "LogSpeed", "WindSpeedRel", 
"WindDirRel", "Course", "SeaDepth", "DraftFWD", "DraftAFT", "Rudder", 
"PropellerKW", "MEConsumption", "MERPM", "MELoad", "DGConsumption", 
"DG1_Load", "DG2_Load", "DG3_Load", "DG4_Load", "DG5_Load"), row.names = c(NA, 
6L), class = "data.frame") 

> dput(head(DataOutput)) 
structure(list(Type = structure(c(1L, 3L, 2L, 4L), .Label = c("#Measurements", 
"NA", "OOR", "Ratio((NA+OOR)/#M"), class = "factor"), Latitude = c(67879, 
0, 19829, 0.292122747830699), Longitude = c(67879, 0, 19829, 
0.292122747830699), GPSSpeed = c(67879, 7, 19904, 0.293330779769885 
), LogSpeed = c(67879, 18235, 49621, 0.999661161773155), WindSpeedRel = c(67879, 
392, 38297, 0.569970093843457), WindDirRel = c(67879, 0, 38297, 
0.564195111890275), Course = c(67879, 0, 67879, 1), SeaDepth = c(67879, 
0, 67879, 1), DraftFWD = c(67879, 0, 67879, 1), DraftAFT = c(67879, 
0, 67879, 1), Rudder = c(67879, 46675, 21204, 1), PropellerKW = c(67879, 
5857, 21332, 0.400550980421043), MEConsumption = c(67879, 10, 
21185, 0.312246792085918), MERPM = c(67879, 5105, 22030, 0.399755447192799 
), MELoad = c(67879, 0, 67879, 1), DGConsumption = c(67879, 0, 
67879, 1), DG1_Load = c(67879, 0, 67879, 1), DG2_Load = c(67879, 
0, 67879, 1), DG3_Load = c(67879, 0, 67879, 1), DG4_Load = c(67879, 
0, 67879, 1), DG5_Load = c(67879, 0, 67879, 1)), .Names = c("Type", 
"Latitude", "Longitude", "GPSSpeed", "LogSpeed", "WindSpeedRel", 
"WindDirRel", "Course", "SeaDepth", "DraftFWD", "DraftAFT", "Rudder", 
"PropellerKW", "MEConsumption", "MERPM", "MELoad", "DGConsumption", 
"DG1_Load", "DG2_Load", "DG3_Load", "DG4_Load", "DG5_Load"), row.names = c(NA, 
4L), class = "data.frame") 

回答

1

我想你可以直接在這裏沒有子集循環:

Data[,DataOutput[4,]!=1] 

,如果你沒有DataOutput並想擺脫列的完全充滿NAs你可以做這樣的事情:

Data[,colSums(is.na(Data))!=nrow(Data)] 
+0

謝謝,這完全解決了我的問題! –

相關問題