> a <- 2:12
> b <- seq(2,23,2)
> c <- c(1:11)^3
> d <- c(5:8, 30:36)
> df <- data.frame(a,b,c,d)
> df$a[df$a==8] <- NA
> df$b[df$b==8] <- NA
> df$c[df$c==8] <- NA
> df$d[df$d==8] <- NA
> df$d[df$d==32] <- NA
> df
a b c d
1 2 2 1 5
2 3 4 NA 6
3 4 6 27 7
4 5 NA 64 NA
5 6 10 125 30
6 7 12 216 31
7 NA 14 343 NA
8 9 16 512 33
9 10 18 729 34
10 11 20 1000 35
11 12 22 1331 36
> bad.d <- is.na(df$d)
> bad.d
[1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
> df[!bad.d,]
a b c d
1 2 2 1 5
2 3 4 NA 6
3 4 6 27 7
5 6 10 125 30
6 7 12 216 31
8 9 16 512 33
9 10 18 729 34
10 11 20 1000 35
11 12 22 1331 36
> df[complete.cases(df[,2:3]),]
a b c d
1 2 2 1 5
3 4 6 27 7
5 6 10 125 30
6 7 12 216 31
7 NA 14 343 NA
8 9 16 512 33
9 10 18 729 34
10 11 20 1000 35
11 12 22 1331 36
> df[complete.cases(df[,c(2,4)]),]
a b c d
1 2 2 1 5
2 3 4 NA 6
3 4 6 27 7
5 6 10 125 30
6 7 12 216 31
8 9 16 512 33
9 10 18 729 34
10 11 20 1000 35
11 12 22 1331 36
> df[complete.cases(df),]
a b c d
1 2 2 1 5
3 4 6 27 7
5 6 10 125 30
6 7 12 216 31
8 9 16 512 33
9 10 18 729 34
10 11 20 1000 35
11 12 22 1331 36
> na.omit(df)
a b c d
1 2 2 1 5
3 4 6 27 7
5 6 10 125 30
6 7 12 216 31
8 9 16 512 33
9 10 18 729 34
10 11 20 1000 35
11 12 22 1331 36
> mean(df$d, na.rm=TRUE)
[1] 24.11111
> x <- c(3:5, 11:8, 8 + 0:5)
> x
[1] 3 4 5 11 10 9 8 8 9 10 11 12 13
> unique(x)
[1] 3 4 5 11 10 9 8 12 13
> unique(x, fromLast = TRUE)
[1] 3 4 5 8 9 10 11 12 13
> d1 <- c(1,1,1,1)
> d2 <- c(2,2,3,2)
> d3 <- c(3,4,5,3)
> d4 <- data.frame(d1,d2,d3)
> d4
d1 d2 d3
1 1 2 3
2 1 2 4
3 1 3 5
4 1 2 3
> unique(d4)
d1 d2 d3
1 1 2 3
2 1 2 4
3 1 3 5
> duplicated(d4)
[1] FALSE FALSE FALSE TRUE
> d6 <- d4[!duplicated(d4),]
> d6
d1 d2 d3
1 1 2 3
2 1 2 4
3 1 3 5
> d7 <- d4[!duplicated(d4[,2]),]
> d7
d1 d2 d3
1 1 2 3
3 1 3 5
本文参考:
1.https://blog.csdn.net/qq_35242986/article/details/68927909
2.http://blog.51cto.com/matrix6ro/1791239
3.https://blog.csdn.net/u010652755/article/details/72979692
4.https://blog.csdn.net/brunowuestelle/article/details/46625253
第四篇本文未涉及,但看见排序了
|
请发表评论