-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path002-other_function_include_which.R
More file actions
88 lines (48 loc) · 1.74 KB
/
002-other_function_include_which.R
File metadata and controls
88 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
### which()函数
#用法 which(test)。
#返回test为真值的位置(指针)
x <- c(1,1,1,0,0,1,1)
which(x!=1) #返回x中不等于1的变量值得位置
which(x>0)
which(c(T,F,T)) #返回c(T,F,T)中为TURE值的位置。
a = data.frame(x=1:10,y=rnorm(10),z=letters[1:10])
which(a$x >5)
#[1] 6 7 8 9 10
> which(a$y > 0)
[1] 1 4 6 8 9
#df条件选值
1) a[which(a$x > 5),]
2) a[6:10,]
3) subset(a,x>5)
> a[which(a$y > 0),]
4) library(dplyr)
filter(a,x>5)
###2020-10-16 补充 数据框筛选
## 1. which筛选
iris[which(iris$Sepal.Width == 2),] %>% head()
iris[which(iris$Species == 'setosa'),] %>% head()
iris[order(iris$Sepal.Width),] %>% head()
##which与order格式很像!
## 2. 多条件筛选
iris[which((iris$Species == 'setosa') | (iris$Species == 'virginica')),]
iris[which(iris$Species %in% c('setosa','virginica')),] #** %in% 操作符,a %in% b 将生成一个与a长度相同的logical序列,依次判断a中的元素是否被包含在b中
## 3. 不用which
iris[iris$Sepal.Width== 2, ]
iris[iris$Sepal.Width> 4, ]
iris[iris$Species== 'setosa', ]
iris[(iris$Species == 'setosa') | (iris$Species == 'virginica'),]
iris[iris$Species %in% c('setosa', 'virginica'),]
## 4. filter() 重点掌握!
library(dplyr)
filter(iris,Sepal.Width == 2)
filter(iris,Sepal.Width > 4)
filter(iris,Species == 'setosa')
filter(iris,Species %in% c('setosa', 'virginica'))
## 5. subset()
###分类汇总
## 1. aggregate() by:分类依据,必须是list形式
aggregate(x=iris$Sepal.Width, by=list(iris$Species),FUN=mean)
## 2. split()函数和sapply()函数
> a <- split(iris$Sepal.Width,iris$Species)
> sapply(a, mean)
## 3.