-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpractice.r
More file actions
83 lines (62 loc) · 1.89 KB
/
practice.r
File metadata and controls
83 lines (62 loc) · 1.89 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
library(plyr)
head(baseball)
View(baseball)
dim(baseball)
str(baseball)
baseball$sf[baseball$year<1954] <- 0
any(is.na(baseball$sf)) #已無NA值
sum(is.na(baseball$hbp)) #看幾個true(幾個na) 377個
#以此為索引抓出NA直
baseball$hbp[is.na(baseball$hbp)]
baseball$hbp[is.na(baseball$hbp)] <- 0
sum(is.na(baseball$hbp)) #已無na直
#只保留一季中打數至少為50的選手
baseball <- baseball[baseball$ab >= 50,]
#新增上壘率的欄位
baseball$OBP <- with(baseball, (h+bb+hbp)/(ab+bb+hbp+sf))
head(baseball, 20)
#這裡是計算該選手當年的上壘率
sum(baseball$h+baseball$bb)
sum(baseball$h)
sum(baseball$bb)
#計算某選手生涯OBP
nrow(as.data.frame((unique(baseball$id)))) #共1118名選手
nrow(baseball) #14828筆歷年資料
#資料內有每個選手每年的數據
any(is.na(baseball$ab))
any(is.na(baseball$bb))
any(is.na(baseball$hbp))
any(is.na(baseball$sf))
any(is.na(baseball$hbp))
any(is.na(baseball$bb))
any(is.na(baseball$ab))
obp <- function(data){
c(OBP= with(data, sum(h+bb+hbp)/sum(ab+bb+hbp+sf)),nothing=with(data,sum(h+bb)/sum(ab+bb)))
#要用c()將結果刮起來,最後ddply出來的結果才會顯示OBP於欄位???????????????,
#why???????????
#生涯OBP,所以要用sum
#資料內有每個選手每年的數據
#with函數,指定一個dataframe,就能直接使用其欄位做運算
}
#使用ddply針對每個選手('id'),計算整個棒球生涯的OBP
careerOBP <- ddply(baseball, .variables = 'id', .fun = obp)
View(careerOBP)
str(careerOBP)
############ 純粹練習
df <- as.data.frame(x =careerOBP$OBP, row.names = unique(careerOBP$id) )
colnames(df)[1] <- "careerOBP"
View(df) ######################
w<- cbind(id=careerOBP$id,
careerOBP =careerOBP[order(careerOBP$OBP, decreasing = TRUE),"OBP"],
rank=1:nrow(careerOBP)) %>% as.data.frame()
View(w)
str(w)
dim(w)
View(careerOBP)
colnames(careerOBP)
a<- matrix(1:16,4)
a
class(a)
b <- 1:5
b
class(b)