Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
1
15census_timor_dataclean
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Paddy Tobias
15census_timor_dataclean
Commits
2f0ca9e2
Commit
2f0ca9e2
authored
8 years ago
by
PTobias
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Scripts/ConvertingXLStoCSV.R
+10
-28
10 additions, 28 deletions
Scripts/ConvertingXLStoCSV.R
Scripts/RenameOldtoNew.R
+7
-7
7 additions, 7 deletions
Scripts/RenameOldtoNew.R
with
17 additions
and
35 deletions
Scripts/ConvertingXLStoCSV.R
+
10
−
28
View file @
2f0ca9e2
...
...
@@ -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
)
}
}
...
...
This diff is collapsed.
Click to expand it.
Scripts/RenameOldtoNew.R
+
7
−
7
View file @
2f0ca9e2
...
...
@@ -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
),]
dat
2
=
dat
[
!
grepl
(
"Based"
,
dat
$
V2
,
fixed
=
FALSE
),]
dat
3
=
dat
2
[
!
grepl
(
"Special"
,
dat
2
$
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
dat
4
=
dat
3
[
!
grepl
(
"TIMOR-LESTE"
,
dat
3
$
V2
,
fixed
=
TRUE
),]
dat
5
=
dat
4
[
!
grepl
(
"TOTAL"
,
dat
4
$
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
=
dat
5
[
rowSums
(
is.na
(
dat
5
))
!=
ncol
(
dat
5
),]
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
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment