【发布时间】:2021-02-19 21:50:20
【问题描述】:
我目前正在尝试计算在我的数据集的每个列中找到的 NA 数量。
我正在运行以下代码:
function(x, df1, df2, ncp, log = FALSE)
apply(Total_HousingData, 2, function(x) {sum(is.na(x))})
这是我的输出:
Id MSSubClass MSZoning LotFrontage LotArea Street
0 0 0 0 0 0
Alley LotShape LandContour Utilities LotConfig LandSlope
0 0 0 0 0 0
Neighborhood Condition1 Condition2 BldgType HouseStyle OverallQual
0 0 0 0 0 0
OverallCond YearBuilt YearRemodAdd RoofStyle RoofMatl Exterior1st
0 0 0 0 0 0
Exterior2nd MasVnrType MasVnrArea ExterQual ExterCond Foundation
0 0 0 0 0 0
BsmtQual BsmtCond BsmtExposure BsmtFinType1 BsmtFinSF1 BsmtFinType2
0 0 0 0 1 0
BsmtFinSF2 BsmtUnfSF TotalBsmtSF Heating HeatingQC CentralAir
1 1 1 0 0 0
Electrical 1stFlrSF 2ndFlrSF LowQualFinSF GrLivArea BsmtFullBath
0 0 0 0 0 2
BsmtHalfBath FullBath HalfBath BedroomAbvGr KitchenAbvGr KitchenQual
2 0 0 0 0 0
TotRmsAbvGrd Functional Fireplaces FireplaceQu GarageType GarageYrBlt
0 0 0 0 0 0
GarageFinish GarageCars GarageArea GarageQual GarageCond PavedDrive
0 1 1 0 0 0
WoodDeckSF OpenPorchSF EnclosedPorch 3SsnPorch ScreenPorch PoolArea
0 0 0 0 0 0
PoolQC Fence MiscFeature MiscVal MoSold YrSold
0 0 0 0 0 0
SaleType SaleCondition SalePrice
0 0 1459
由于某种原因,所有 NA 计数都计入 SalePrice 变量。当我查看其他变量时,有很多 NA。我尝试考虑适当的变量,但这仍然没有解决问题。
例如“Alley”应该读为 1,但它的 NA 没有被拾取。
以下是代码示例:
Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape LandContour Utilities
<dbl> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <chr>
1 1 60 RL 65 8450 Pave NA Reg Lvl AllPub
2 2 20 RL 80 9600 Pave NA Reg Lvl AllPub
3 3 60 RL 68 11250 Pave NA IR1 Lvl AllPub
4 4 70 RL 60 9550 Pave NA IR1 Lvl AllPub
5 5 60 RL 84 14260 Pave NA IR1 Lvl AllPub
6 6 50 RL 85 14115 Pave NA IR1 Lvl AllPub
【问题讨论】:
-
尝试
sapply(df, function(colValues) sum(is.na(colValues)))并与您的数据框交换df。如果您输入data.frame,sapply函数会自动循环遍历列。 -
你能给我们至少一份你的数据样本吗?我无法用模拟数据框复制您的问题。使用
dput(head(Total_HousingData))。 -
@Jan 我刚刚添加了这个数据的头部。如您所见,“Alley”有很多 NA,但它们没有在 is.na 搜索中注册。
-
@Jonas 不幸的是,这会产生相同的输出。如果有帮助的话,我会发布一些我正在使用的数据。
-
你为什么接受一个不起作用的答案? ...无论如何,有没有可能
character列Alley包含"NA"作为字符串而不是NA?
标签: r dataframe na data-wrangling