> ID <- paste("A","00","q",1:5,sep = "+")
> ID
[1] "A+00+q+1" "A+00+q+2" "A+00+q+3" "A+00+q+4" "A+00+q+5"
> ID <- paste("A","00",1:5,sep = "")
> ID
[1] "A001" "A002" "A003" "A004" "A005"
> age<-c(24,NA,35,19,-20)
> age
[1] 24 NA 35 19 -20
> df1<-data.frame(ID,age)
> df1
ID age
1 A001 24
2 A002 NA
3 A003 35
4 A004 19
5 A005 -20
> #判别有无缺失值
> is.na(df1)
ID age
[1,] FALSE FALSE
[2,] FALSE TRUE
[3,] FALSE FALSE
[4,] FALSE FALSE
[5,] FALSE FALSE
> #将不合理的值定义为缺失值
> df1$age[df1$age<0] <- NA
> df1
ID age
1 A001 24
2 A002 NA
3 A003 35
4 A004 19
5 A005 NA
> #替换缺失值
> df1[is.na(df1)]<-mean(df1$age,na.rm = T)#rm是remove的意思,移除na值
> df1
ID age
1 A001 24
2 A002 26
3 A003 35
4 A004 19
5 A005 26
> #omit函数去除na值
> ID <- paste("A","00",1:5,sep = "")
> age<-c(24,NA,35,19,-20)
> df1<-data.frame(ID,age)
> df1
ID age
1 A001 24
2 A002 NA
3 A003 35
4 A004 19
5 A005 -20
> df1$age[df1$age<0] <- NA
> df1
ID age
1 A001 24
2 A002 NA
3 A003 35
4 A004 19
5 A005 NA
> na.omit(df1)#omit会将na值所有行都删除
ID age
1 A001 24
3 A003 35
4 A004 19
> #判断与转换函数
> a<-c(1,2,3)
> a
[1] 1 2 3
> b<-c("a","b","c“)
+ b
+ d<-c("1","2")
Error: unexpected numeric constant in:
"b
d<-c("1"
> c
age country
1 1 Chian
2 5 India
> e<-matrix(1:6,2,3)
> e
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> e<-matrix(1:6,ncol=2)
> e
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> f<-c(1:10)
> f
[1] 1 2 3 4 5 6 7 8 9 10
> is.character(b)
[1] FALSE
> is.numeric(a)
[1] TRUE
> is.data.frame(df1)
[1] TRUE
> as.numeric(d)
[1] 1 2
> as.character(a)
[1] "1" "2" "3"
> df1
ID age
1 A001 24
2 A002 NA
3 A003 35
4 A004 19
5 A005 NA
> as.character(df1)
[1] "1:5" "c(24, NA, 35, 19, NA)"
> is.vector(a)
[1] TRUE
> is.matrix(e)
[1] TRUE
> as.matrix(f,nrow=2)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10