-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreadTxt_using_readfwf.R
171 lines (129 loc) · 5.79 KB
/
readTxt_using_readfwf.R
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
###Exercise #1: Redo reading in txt files, but do so with read.fwf
setwd("C:/MyGitRepos/cherry-blossom-run/Data")
trimBlanks <- function(charVector){
nameClean <- gsub("^[[:blank:]]+", "", charVector)
nameClean <- gsub("[[:blank:]]+$", "", nameClean)
nameClean <- gsub("[[:blank:]]+", " ", nameClean)
}
convertTime <- function(charTime){
#takes time in h:mm:ss format and converts it to minutes
timePieces <- strsplit(charTime, ":")
timePieces <- sapply(timePieces, as.numeric)
runTime <- sapply(timePieces,
function(x){
if (length(x) == 2) {x[1] + x[2]/60}
else {60*x[1] + x[2] + x[3]/60}
})
}
read_txt_fwf <- function(M = T, yr, widths,
colNames = c("name", "age", "home", "gun", "net"),
skipRows){
dirName <- ifelse(M, "MenTxt", "WomenTxt")
file <- file.path(dirName, paste0(yr, ".txt"))
df <- read.fwf(file, widths = widths, col.names = colNames,
skip = skipRows, comment.char = "")
#add column to keep track of year and sex
df$year <- yr
df$sex <- ifelse(M, "M", "W")
#keep only one time column - first look for net, then gun, then time
useTime <- if("net" %in% colNames) {
"net"
} else if("gun" %in% colNames) {
"gun"
} else {
"time"}
df <- df[, c("year", "sex", "name", "age", "home", useTime)]
names(df)[names(df) == useTime] <- "runTime"
df$name <- gsub("[,.]", "", trimBlanks(as.character(df$name)))
df$age <- as.integer(df$age)
df$home <- gsub("[,.`]", "", trimBlanks(as.character(df$home)))
#Remove # and * and blanks from time, drop records w/o time, and then convert to minutes
df$runTime <- gsub("[#\\*[:blank:]]", "", as.character(df$runTime))
df <- df[df$runTime != "", ]
df$runTime <- convertTime(df$runTime)
#Remove rows with no name
df <- df[!is.na(df$name), ]
return(df)
}
menWidths <- list(c(-16, 22, 3, 19, 8),
c(-22, 22, 3, 19, 9, 8),
c(-12, 22, 3, 19, 8, 8),
c(-12, 22, 3, 19, 8, 8),
c(-22, 30, 3, 20, 9, 8),
c(-22, 30, 3, 20, 8, 8),
c(-16, 23, 3, 19, 8, 8),
c(-22, 23, 3, 24, 9, 6),
c(-25, 23, 3, 19, 9),
c(-25, 23, 3, 19, -28, 8),
c(-25, 23, 3, 21, 9, 9),
c(-25, 23, 3, 21, -8, 8, 9),
c(-25, 23, 3, 21, -8, 8, 8),
c(-25, 23, 3, 21, -8, 8)
)
menColNames <- list(c("name", "age", "home", "time"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "time"),
c("name", "age", "home", "time"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "time")
)
menSkipRows <- list(3, 2, 5, 3, 3, 8, 8, 9, 7, 8, 8, 8, 8, 8)
names(menWidths) <- 1999:2012
names(menColNames) <- 1999:2012
names(menSkipRows) <- 1999:2012
womenWidths <- list(c(-16, 22, 3, 19, 8),
c(-22, 22, 3, 19, 9, 8),
c(-12, 22, 3, 19, 8, 8),
c(-12, 22, 3, 19, 8, 8),
c(-22, 30, 3, 20, 9, 8),
c(-22, 30, 3, 20, 8, 8),
c(-16, 23, 3, 19, 8, 8),
c(-22, 23, 3, 24, 9, 6),
c(-25, 23, 3, 19, 9),
c(-25, 23, 3, 19, -28, 8),
c(-25, 23, 3, 21, 8, 9),
c(-25, 23, 3, 21, -8, 8, 9),
c(-25, 23, 3, 21, -8, 8, 8),
c(-25, 23, 3, 21, -8, 8)
)
womenColNames <- list(c("name", "age", "home", "time"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "net", "gun"),
c("name", "age", "home", "time"),
c("name", "age", "home", "time"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "gun", "net"),
c("name", "age", "home", "time")
)
womenSkipRows <- list(3, 2, 5, 3, 3, 8, 8, 9, 7, 8, 8, 8, 8, 8)
names(womenWidths) <- 1999:2012
names(womenColNames) <- 1999:2012
names(womenSkipRows) <- 1999:2012
#must use SIMPLIFY = F to return list of dataframes;
#if not, it will try to reduce the result to a matrix
menDF_fwf <- mapply(read_txt_fwf, M = T, yr = 1999:2012, widths = menWidths,
colNames = menColNames, skipRows = menSkipRows,
SIMPLIFY = F)
womenDF_fwf <- mapply(read_txt_fwf, M = F, yr = 1999:2012, widths = womenWidths,
colNames = womenColNames, skipRows = womenSkipRows,
SIMPLIFY = F)
#combine each DF in list into one big DF
cbMen_fwf <- do.call(rbind, menDF_fwf)
cbWomen_fwf <- do.call(rbind, womenDF_fwf)
#save DFs
save(cbMen_fwf, file = "cbMen_fwf.rda")
save(cbWomen_fwf, file = "cbWomen_fwf.rda")