#缺失值
An=c (1,2,na)
is.na (AN) #会形成一个布尔向量
A Boolean vector is a vector such as a herd (false,false,ture).
There is also a function for missing values: complete.cases function
The difference between this function and Is.na is:
1, the output data format is different. Is.na form A (false,false,ture) column according to the Data box format, and complete.cases form is a sequence vector, no longer in accordance with the data box format;
2, the output data content is different. The logical vector of the complete.cases output is the exact opposite of the is.na, is.na ture is the missing value, and complete.cases is the full value.
An=c (1,2,na)
sum (complete.cases (AN))
sum (!complete.cases (a)) #==sum (Is.na (AN))
mean (! Complete.cases (AN)) #获得缺失比例 ==1/3
Doubt: Why Boolean vector, sum can get a numerical value. --Because R defaults to Ture, false as 1, 0
#计算缺失值个数
SUM (is.na) #单数列, sum the value of "ture" can be directly computed and
colsums (Is.na (AN), na.rm = T) #多维数列, by column, Na.rm for the need to ignore missing values, na.rm=t to ignore, delete
rowsums (Is.na (AN), na.rm = T) #多维数列, by row, na.rm whether missing values need to be ignored, na.rm=t to ignore, delete
#数据框中的缺失值操作
#数据框中的缺失值操作
Y <-an[is.na (a)] #选中缺失值
y<- an[is.na (AN) = = "TRUE" #上同, select the missing value
an[is.na (AN)] <-0 # indicates that the NA element in vector x is 0来 instead of
an[(!is.na (AN)) & X>0]-> z #可以用 & Add other conditions for filtering
————————————————————————————————————————————————————————————
Missing value Detection solution:
The detection of missing values should include: Number of missing values, percentage of missing values, missing values, and full value data filtering.
#缺失值解决方案
sum (complete.cases (saledata)) #is. NA (saledata)
sum (!complete.cases (saledata))
Mean (!complete.cases (saledata)) #1/201 digit, missing value proportional
saledata[!complete.cases (saledata),] #筛选出缺失值的数值