因为x中有NA,所以当对x进行sum操作时,y会被赋值为NA,那如何才能在计算时去除NA的干扰呢?
y<-sum(x,na.rm=TRUE)
通过在函数中加“na.rm=TRUE”即可,书中说很多函数都可以用这个方法去除NA的干扰,大家可以试一下。
如果想要去除数据中的NA,可以这样:
newdata<-na.omit(olddata)
#前二种算法可以
计算
缺失数据,但随机森林不行,所以还需将数据进行清洗整理
data(algae)
algae <- algae[-many
NA
s(algae,0.2), ]#占有20%的
NA
值
的行
去掉
clean.algae <- knnImputation(algae,k=10)#平均
值
填充
NA
值
#回归树模型
计算
model.tree=rpart(a1 ~ ., data = clean.algae[, 1:12])
summary(model.tree)
pre.tree <- predict(model.tree, clean.algae)
plot(pre.tree~clean.algae$a1)
nmse1 <- mean((pre.tree- clean.algae[,'a1'])^2)/mean((mean(clean.algae[,'a1'])- clean.algae[,'a1'])^2)
nmse1
主要是通过
R语言
,对日期数据进行处理,并补全缺失数据
rawdata<- read.csv("C:/Users/li/Desktop/ss.csv",fill=F)
#提取数据 ss1,并组合数据-------------------------------
ts1<-rawdata$ts1
ts11<-as.Date(ts1,'%Y/%m/%d')
false<-is.
na
(ts11)
ts21<-ts11[!false]
ss1<-rawdata$SS1
ss1<-ss1[!false]
library(zoo)
data1<-zoo(ss1,ts21)
#补全不规则数据(时间的缺失和缺失
值
)
date1<-zoo(,seq(start(data1),end(data1),'day'))
datanew1<-merge(data1,date1)
datanew1[is.
na
(datanew1)]<-median(datanew1,
na
.rm = T)
#提取数据 ss2
R语言
剔除包含
NA
的行或列前言
NA
值
的影响
去除
NA
值
在数据分析的过程
中
,数据的前处理是非常重要的。数据
中
出现“
NA
”是非常常见的,“
NA
”指“Not Available”,出现
NA
常常会影响我们进行数据分析,这是因为
NA
是会传染的。
NA
值
的影响
看以下例子:
> a <- c(1,2,3,4,5)
> mean(a)
[1] 3
如果出现
NA
,情况就不一样了
> a <- c(1,2,3,4,5,
NA
)
> mean(a)
[1]
NA
由此可见,
NA
值
目录00引言1、Inf2、
NA
N(Not a Number)3、
NA
与逻辑运算符4、总结
这篇的主角是
R语言
中
缺失
值
(
NA
)的识别与提取。先介绍
NA
N、Inf及其运算。在最终介绍缺失
值
(
NA
),毕竟重要的都压轴嘛。
1、Inf
在实数轴上除了确定的有限点,还有两个无限:正无穷、负无穷。用Inf、-Inf表示。
[1] Inf
[1] -Inf
>...
R语言
处理缺失
值
在处理数据过程
中
,避免不了会产生一些缺失
值
,如未填写数据或者编码错误等原因,用
NA
表示缺失
值
。在
R语言
中
,is.
na
()函数可以判断元素是否是缺失
值
,从而返回逻辑
值
(TRUE/FALSE),所以该函数将会返回和元数据集一样大小的数据集。在判断缺失
值
的过程
中
,需要注意以下两点:
一是缺失
值
是不可以比较的,即不可以用缺失
值
去寻找缺失
值
,如var ==
NA
返回的结果永远不会是true。
二是
R语言
中
不会将正无穷和负无穷写成
NA
,分别用 Inf 和 –Inf 所标记。
既然缺失
值
可能无处不在,那么在数据分析过程
中
可以采取如下的方法
去除
缺失
值
:
一是很多数
值
函数都拥有一个
na
.rm
# for the first 15 colomns ==========
dat=read.table("data1.txt",head=TRUE)
na
=col
na
mes(dat)
dat1=dat[,
na
[-1]]
nc=dim(dat1)[2] # 列数
nr=dim(dat1)[1] # 行数
logr=-dat1
Data A
na
lysis for Titanic Survivors
The survival rate is influenced by a person’s gender, age, Pclass, and ticket fare. So, the data
are used to allude that generally healthy full-grown male adults sacrifice themselves for the
lives of women, children, and the elderly in times of disaster.
Description: Describe the methods (or tools) you used to a
na
lyze the features and why
they can be helpful. Also, give a brief introduction of
Na
ïve Bayes models (in your own
words).
Our team used R language
一般在项目
中
,数据可能会因为设备故障、未作答问题或误编码数据的原因不完整。在R
中
NA
(not available,不可用)表示缺失
值
。
函数is.
na
()允许你检测缺失
值
是否存在。该函数作用于检测对象之后将返回一个相同大小的对象,其
中
缺失
值
的位置被改写为true,其他不是缺失
值
的位置则为false。
> which(is.
na
(nhanes2)) #返回缺失
值
的位置
> sum(is.
na
data_frame$column_
na
me <- data_frame$column_
na
me[!is.
na
(data_frame$column_
na
me)]
其
中
,data_frame是指数据框名,column_
na
me是指需要
去除
NA
值
的列名。
此代码会将数据框
中
某列
中
的所有
NA
值
去除
,并重新赋
值
给该列,将不为空的
值
保留下来。