我目前使用的是一个2个数据帧,一个叫做Data,另一个叫做DataOutput。数据有超过40万个21个变量的观测值,而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")
答案 0 :(得分:1)
我认为你可以在没有循环的情况下直接在这里进行分组:
Data[,DataOutput[4,]!=1]
并且如果你没有DataOutput
并且想要摆脱专门用NA填充的列,你可以做这样的事情:
Data[,colSums(is.na(Data))!=nrow(Data)]