forked from datadolphyn/R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassifyData.R
267 lines (241 loc) · 9.17 KB
/
ClassifyData.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Classify Data
# Author: Jitender Aswani, Co-Founder @datadolph.in
# Date: 3/15/2013
# Copyright (c) 2011, under the Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0) License
# For more information see: https://creativecommons.org/licenses/by-nc/3.0/
# All rights reserved.
#
# Generate extra date cols
#
generateExtraDateCols <- function(data, col.index) {
lMonths <- c("January","February","March", "April","May","June","July","August","September", "October","November","December")
lDays <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
#data$Day_generated=factor(weekdays(data[,col.index]), levels=lDays, ordered=TRUE)
data$Day_g=factor(weekdays(data[,col.index]), levels=lDays)
#data$Month_generated=factor(months(data[,col.index]), levels=lMonths, ordered=TRUE)
data$Month_g=factor(months(data[,col.index]), levels=lMonths)
data$Quarter_g=as.factor(quarters(data[,col.index]))
data$Year_g=as.factor(years(data[,col.index]))
#data$Year=as.numeric(format(data[,col.index], "%Y"))
#as.numeric(format(date1, "%m"))
#format(date1, "%b") ## Month, char, abbreviated
return(data)
}
#
# Define all supported date formats
#
getAllowedDateFormats <- function() {
#d = '([0 ]?[1-9]|[12][0-9]|3[01])' # 1-31 with leading zero or space for single digits
#m = '([0 ]?[1-9]|1[0-2])' # 01-12 with leading zero or space for single digits
d = '([0][1-9]|[12][0-9]|3[01])' # 1-31 with leading zero for single digits
m = '([0][1-9]|1[0-2])' # 01-12 with leading zero for single digit
Y = '(17[0-9][0-9]|18[0-9][0-9]|19[0-9][0-9]|20[0-9][0-9])' # 1700 - 2099
#Y = '(19[0-9][0-9]|20[0-9][0-9])' # 1900 - 2099
y = '([0-9][0-9])' # 00-99
b='([a-zA-Z]{3})'
B='([a-zA-Z]+)'
#q = '([qQ][1-4])'
#patterns <- c("%Y%m%d", "%m%d%y", "%m%d%Y", "%d%m%y", "%d%m%Y", "%y%m%d")
#patterns <- c("Ymd", "mdy", "mdY", "dmy", "dmY", "ymd", "y", "Y", "by", "bY", "By", "BY")
#removed %y from the list to avoid getting numerical column identified, moved %Y at the front to avoid 4 digit year being classified as %d%m%y
patterns <- c("Y","Ymd", "mdy", "mdY", "dmy", "dmY", "ymd","by", "bY", "By", "BY")
#patterns <- c("Y")
separators <- c("", " ", "/", "-", ".")
#strptime {base} /^[a-zA-Z]{3}\s+[0-9]{2}$/
allowed.date.list <- list()
for (i in 1:length(patterns)) {
for (j in 1:length(separators)) {
p <- patterns[i]
s <- separators[j]
pattern.length <- nchar(p)
chars <- substring(p,seq(1, pattern.length), seq(1, pattern.length))
m.chars <- paste("%", chars, sep="")
format <- paste(m.chars,collapse=s)
if(!s %in% c("", " ")){
s <- paste("\\", s, "", sep="" )
}
regex <- ''
if( pattern.length == 3) {
regex <- paste('^', eval(get(chars[1])), s, eval(get(chars[2])), s, eval(get(chars[3])), '$', sep="")
} else if ( pattern.length == 2) {
regex <- paste('^', eval(get(chars[1])), s, eval(get(chars[2])),'$', sep="")
} else if ( pattern.length == 1) {
regex <- paste('^', eval(get(chars[1])),'$', sep="")
}
allowed.date.list[[regex]] = format
}
}
return (allowed.date.list)
}
#
# get date format for sample of values
#
getDateFormat <- function(sample.dates) {
allowed.date.list <- getAllowedDateFormats()
#sample.dates <- sample.dates[!is.na(sample.dates)]
for(i in 1:length(allowed.date.list)) {
#Check how many items matced a date pattern
matched <- sum(grepl(names(allowed.date.list)[i], sample.dates))
if(matched == length(sample.dates)) {
#print(names(allowed.date.list)[i])
return(allowed.date.list[[i]])
}
}
return(NULL)
}
#Test this function
testDates <- function() {
#dates <- c("March 2010","December 2011", "February 2012", "March 2014", "Jan 2013")
dates <- c("1962", "1949", "1932", "1963", "1925")#, 1937, 1912, 1944, 1930, 1958, 1939, 1919, 1915)
#dates <- seq(as.Date("2000/1/1"), by="month", length.out=1000)
#Remove NA: dates <- dates[!is.na(dates)]
date.format <- getDateFormat(dates)
#d <- as.Date(dates, date.format)
if(date.format %in% c("%y", "%Y")) {
dates = as.Date(paste("1 1 ", dates, sep=""), format=paste("%d %m ", date.format, sep=""))
} else if (date.format %in% c("%b%y","%b %y", "%b/%y", "%b-%y", "%b.%y",
"%b%Y", "%b %Y", "%b/%Y", "%b-%Y", "%b.%Y",
"%B%y", "%B %y", "%B/%y", "%B-%y", "%B.%y",
"%B%Y", "%B %Y", "%B/%Y", "%B-%Y", "%B.%Y")) {
sep <- gsub("%[a-zA-Z]{1}(.*)%[a-zA-Z]{1}", "\\1", date.format)
dates = as.Date(paste("1", dates, sep=sep), format=paste("%d", date.format, sep=sep))
} else {
dates = as.Date(dates, format=date.format)
}
}
#
# Test if date
#
isDate <- function(col, colname="date") {
allowed.date.headers <- c("date", "time", "index")
#if(tolower(colname) %in% allowed.date.headers || grepl("[date]")) {
# print("date found in the header")
#}
return (getDateFormat(col))
}
#
# Test if logical
#
isColLogical <- function(col) {
if(length(levels(factor(col))) == 2) {
bool.types <- c('true', 'false', 't', 'f', '1', '0', 'y', 'n', 'yes', 'no')
countBool <- sum(sapply(tolower(col), function(x) x %in% bool.types), na.rm=TRUE)
if (countBool == length(col))
return(TRUE)
else
return(FALSE)
} else
return (FALSE)
}
#
#test if number
#
isNumber <- function(col) {
countBool <- sum(sapply(col, is.numeric), na.rm=TRUE)
if (countBool == length(col))
return(TRUE)
else
return(FALSE)
}
#
#Main funciton which takes a column (or a vector) and attempts to classify it...
#
classifyData <- function(data) {
#Default sample size
sample.size <- 30
if(nrow(data) <= 30)
sample.size <- nrow(data)
#Remove rows that have NA
if(ncol(data) == 1) { # this is an intersting one - if pad has only one column, the above line returns a vector rather than a data.frame hence this loop
col.names <- colnames(data)
data <- as.data.frame(data[rowSums(is.na(data)) != ncol(data),])
#Take a small sample
data.sample <- as.data.frame(data[sample(1:nrow(data)[1], size=sample.size, replace=FALSE),])
colnames(data) <- col.names
} else {
data <- data[rowSums(is.na(data)) != ncol(data),]
#Take a small sample
data.sample <- data[sample(1:nrow(data)[1], size=sample.size, replace=FALSE),]
}
cls <- sapply(data.sample, class)
#print(cls)
classified <- list()
col.names <- colnames(data.sample)
for (i in 1:ncol(data.sample)) {
col <- data.sample[,i]
col <- col[!is.na(col)] #remove NA
#check for boolean
if(isColLogical(col)) {
#storage.mode(data[,i]) = "logical"
data[,i] = as.factor(data[,i])
#classes.classified[[i]] = "logical"
next
}
#check for date names (in date, time)
date.format <- isDate(col)
#print(date.format)
if(!is.null(date.format)) {
if(date.format %in% c("%y", "%Y")) {
data[,i] = as.Date(paste("1 1 ", data[,i], sep=""), format=paste("%d %m ", date.format, sep=""))
} else if (date.format %in% c("%b%y","%b %y", "%b/%y", "%b-%y", "%b.%y",
"%b%Y", "%b %Y", "%b/%Y", "%b-%Y", "%b.%Y",
"%B%y", "%B %y", "%B/%y", "%B-%y", "%B.%y",
"%B%Y", "%B %Y", "%B/%Y", "%B-%Y", "%B.%Y")) {
sep <- gsub("%[a-zA-Z]{1}(.*)%[a-zA-Z]{1}", "\\1", date.format)
data[,i] = as.Date(paste("1", data[,i], sep=sep), format=paste("%d", date.format, sep=sep))
} else {
data[,i] = as.Date(data[,i], format=date.format)
generateExtraDateCols(data, i)
}
next
}
#check for int
if (isNumber(col)){
data[,i] = as.numeric(data[,i])
next
}
#check for factor
}
if(verbose) print("Classification Done!")
logMessage("Classification Done!")
return(data)
}
testClassification <- function() {
#l <- c(1,0,1,0,0,0,1,0)
#z <- sample(c(TRUE,FALSE),1000000,rep=TRUE)
#z <- tolower(z)
#Geneates a matrix
#date.patterns <- unlist(
# lapply(separtors, function(y)
# lapply(patterns, function(x) paste(substring(x,seq(1,nchar(x), 2), seq(2,nchar(x), 2)),
# collapse=y)
# )))
#sample.df <- data.frame(bigmove=sample(c("y","n"),1000,rep=TRUE),
# date=seq(as.Date("2000/1/1"), by="month", length.out=1000),
# value=rep(1:100,100), stringsAsFactors = FALSE)
sample.df <- data.frame(dates=c("1-31-2012", "1-1-2011", "1-1-2010", "1-1-2009"), pass=c(1,0,1,0), name=c("a", "bsd", "adaads", "dafds"), stringsAsFactors = FALSE)
print(sapply(sample.df, class))
data <- classifyData(sample.df)
print(sapply(data, class))
}
if(FALSE)
{
#Symbol Meaning Example
#%d day as a number (0-31) 01-31
#%a
#%A abbreviated weekday
#unabbreviated weekday Mon
#Monday
#%m month (00-12) 00-12
#%b
#%B abbreviated month
#unabbreviated month Jan
#January
#%y
#%Y 2-digit year
#4-digit year 07
#2007
# metaChar = c("$","*","+",".","?","[","^","{","|","(","\\")
#
#`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]
}