-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify PHB functionality to just read and merge #68
- Loading branch information
1 parent
2ceb92c
commit 3eab460
Showing
5 changed files
with
55 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,40 @@ | ||
mergePHBfilePairs = function(inputPath = ".", outputPath = ".", | ||
mergePHBfilePairs = function(filenames = NULL, | ||
timeformat = "%m/%d/%Y %H:%M:%S", | ||
desiredtz = "", configtz = NULL, | ||
timeformatName = "timeformat") { | ||
# merges Philips Health Band xlsx files per participant | ||
# as there can be multiple files per participant. | ||
fnames = dir(inputPath, recursive = FALSE, full.names = TRUE, pattern = "[.]xlsx") | ||
fileOverview = data.frame(filename = fnames) | ||
extractID = function(x) { | ||
x = basename(x) | ||
x = gsub(pattern = "sleep_wake", replacement = "sleepwake", x = tolower(x)) | ||
ID = unlist(strsplit(x, "_"))[2] | ||
return(ID) | ||
if (length(filenames) != 2) { | ||
stop("Provide two filenames") | ||
} | ||
fileOverview$ID = unlist(lapply(fileOverview$filename, FUN = extractID)) | ||
|
||
uids = unique(fileOverview$ID) | ||
for (uid in uids) { | ||
filesForThisPerson = fileOverview$filename[which(fileOverview$ID == uid)] | ||
# Identify both file | ||
file1 = grep(pattern = "datalist", x = filesForThisPerson, ignore.case = TRUE) | ||
file2 = grep(pattern = "sleep_wake", x = filesForThisPerson, ignore.case = TRUE) | ||
if (length(file1) == 0 && length(file2) == 0) { | ||
next | ||
} | ||
# Data | ||
deviceSN = NULL | ||
# Identify both file | ||
file1 = grep(pattern = "datalist", x = filenames, ignore.case = TRUE) | ||
file2 = grep(pattern = "sleep_wake", x = filenames, ignore.case = TRUE) | ||
|
||
# Datalist file (with all variables except sleep/wake scores) | ||
deviceSN = NULL | ||
if (length(file1) > 0) { | ||
data1 = readPHBCount(filename = filenames[file1], timeformat = timeformat, | ||
desiredtz = desiredtz, configtz = configtz, | ||
timeformatName = timeformatName) | ||
deviceSN = data1$deviceSN | ||
} | ||
# Sleep wake scores file | ||
if (length(file2) > 0) { | ||
data2 = readPHBCount(filename = filenames[file2], timeformat = timeformat, | ||
desiredtz = desiredtz, configtz = configtz, | ||
timeformatName = timeformatName) | ||
} | ||
if (length(file1) > 0 && length(file2) > 0) { | ||
data2$data = data2$data[, which(colnames(data2$data) != "sleepEventMarker")] | ||
data = merge(data1$data, data2$data, by = "timestamp") | ||
} else { | ||
if (length(file1) > 0) { | ||
data1 = readPHBCount(filename = filesForThisPerson[file1], timeformat = timeformat, | ||
desiredtz = desiredtz, configtz = configtz, | ||
timeformatName = timeformatName) | ||
deviceSN = data1$deviceSN | ||
} | ||
# Sleep wake scores | ||
if (length(file2) > 0) { | ||
data2 = readPHBCount(filename = filesForThisPerson[file2], timeformat = timeformat, | ||
desiredtz = desiredtz, configtz = configtz, | ||
timeformatName = timeformatName) | ||
} | ||
if (length(file1) > 0 && length(file2) > 0) { | ||
data2$data = data2$data[, which(colnames(data2$data) != "sleepEventMarker")] | ||
data = merge(data1$data, data2$data, by = "timestamp") | ||
data = data1$data | ||
} else { | ||
if (length(file1) > 0) { | ||
data = data1$data | ||
} else { | ||
data = data2$data | ||
} | ||
data = data2$data | ||
} | ||
colnames(data)[grep(pattern = "timestamp", x = colnames(data))] = "timestamp" | ||
newName = gsub(pattern = "Sleep_Wake", replacement = "def", x = basename(filesForThisPerson[file2]), ignore.case = TRUE) | ||
newName = paste0(unlist(strsplit(newName, "[.]")) , collapse = paste0("_", deviceSN, ".")) | ||
newName = gsub(pattern = "xlsx", replacement = "csv", x = newName) | ||
outputfile = paste0(outputPath, "/", newName) | ||
write.csv(x = data, file = outputfile, row.names = FALSE) | ||
} | ||
invisible(list(data = data, deviceSN = deviceSN)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.