Skip to content
Snippets Groups Projects
Commit 2f0ca9e2 authored by PTobias's avatar PTobias
Browse files

bug fixes in Converting script and updated to match working directories

parent b17e5304
No related branches found
No related tags found
No related merge requests found
......@@ -9,55 +9,37 @@ importWorksheets <- function(filename) {
sheet_list <- lapply(sheet_names, function(.sheet){
readWorksheet(object=workbook, .sheet)})
}
# wb = importWorksheets("1_2015-V2-Population-Household-Distribution.xls")
#working on reading in multiple files
# wbs = list.files(pattern = "*.xls")
# creating a function to get sheet names
# newfilename = function(filename) {
# workbook <- loadWorkbook(filename)
# sheet_names <- getSheets(workbook)
# }
# write csv for second sheet with sheet name
# write.csv(wb[2], sheet_names[2])
# and third
# write.csv(wb[3], sheet_names[3])
#wb = importWorksheets("data/1_2015-V2-Population-Household-Distribution.xls")
#filename = "data/1_2015-V2-Population-Household-Distribution.xls"
#output = "output/OldName/"
#writting csv files from one Excel workbook
csv = function(filename, output = NULL) {
#input:
# filename: character string of a xls file name
# output: character string of the saved csv
wb = importWorksheets(filename)
gsub(",","",wb)
if(!is.null(output)) {
for (x in seq_along(wb)) {
# getting sheet names (to write csv with sheet name)
workbook <- loadWorkbook(filename)
sheet_names <- getSheets(workbook)
#writing csv with sheet names as filename
df = gsub(",","",wb[x])
df = wb[x]
write.csv(df, file = paste(output,sheet_names[x], ".csv", sep = ""))
}
}
}
csv("data/1_2015-V2-Population-Household-Distribution.xls", output = "output/OldName/")
csv_all = function(pattern) {
dir = "output/OldName/"
filenames = list.files(pattern = pattern)
output = "output/OldName/"
input = "data/"
filenames = list.files(path = input, pattern = pattern)
for (f in filenames) {
csv_name = paste(dir,f,sep="")
csv(f, output = csv_name)
csv_name = paste(output,f,sep="")
csv(paste(input,f,sep=""), output = csv_name)
}
}
......
......@@ -8,16 +8,16 @@ rename = function(filename, output = NULL) {
newname = x[1,2]
x[3,2] <- x[1,2]
#delete first row and first column that hold legacy names
y=x[2:nrow(x), 2:ncol(x)]
x=x[2:nrow(x), 2:ncol(x)]
#delete all rows with unwanted words in the second column
dat = y[!grepl("(1)", y$V2, fixed = TRUE),]
dat2 = dat[!grepl("Based", dat$V2, fixed = FALSE),]
dat3 = dat2[!grepl("Special", dat2$V2, fixed = FALSE),]
dat = x[!grepl("(1)", x$V2, fixed = TRUE),]
dat = dat[!grepl("Based", dat$V2, fixed = FALSE),]
dat = dat[!grepl("Special", dat$V2, fixed = FALSE),]
#delete all 'totals' rows
dat4 = dat3[!grepl("TIMOR-LESTE", dat3$V2, fixed = TRUE),]
dat5 = dat4[!grepl("TOTAL", dat4$V2, fixed = TRUE),]
dat = dat[!grepl("TIMOR-LESTE", dat$V2, fixed = TRUE),]
dat = dat[!grepl("TOTAL", dat$V2, fixed = TRUE),]
#to clean all rows complete with NAs
clean = dat5[rowSums(is.na(dat5)) != ncol(dat5),]
clean = dat[rowSums(is.na(dat)) != ncol(dat),]
#clean up NA with value below
#clean[is.na(clean)] <- 0
cleanNA = t(na.locf(t(clean), fromLast = FALSE))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment