R language ︱ missing value processing _r︱ data manipulation and cleaning

Source: Internet
Author: User


#缺失值

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),]  #筛选出缺失值的数值


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.