我目前正在使用 2 个数据帧,一个称为 Data,另一个称为 DataOutput。数据有超过 40 万个 21 个变量的观测值,而 DataOutput 只有 4 个 21 个变量的观测值。DataOutput 是一个数据框,它包含不同的总和,包括多少 NA 和 OOR(OutofRange) 以及 #Measurements 和 Ratio((NA+OOR)/#Measurements)。Data 数据框目前包含许多仅包含 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")