Skip to content

Instantly share code, notes, and snippets.

@JakeConway
Last active December 27, 2016 06:12
Show Gist options
  • Save JakeConway/e5d65ada53ea414459e8e37bdcee5cde to your computer and use it in GitHub Desktop.
Save JakeConway/e5d65ada53ea414459e8e37bdcee5cde to your computer and use it in GitHub Desktop.
Script to remove dates from files and convert them to days since initial visit
date_to_day <- function(file_name, first_date){
data <- read.table(file_name, header=TRUE, sep='\t', quote = NULL, row.names = NULL, check.names = FALSE)
data$Date <- as.character(data$Date)
data <- data[order(as.Date(data$Date, "%m/%d/%y")), ]
data$Date <- as.Date(data$Date, "%m/%d/%y")
if(is.null(first_date) == TRUE){
first_date <- data[1, ]$Date
}
else{
first_date <- as.Date(first_date, "%m/%d/%y")
}
Day <- rep(NA, times=nrow(data))
data$Day <- Day
for(i in seq(nrow(data))){
data[i, ]$Day <- data[i, ]$Date - first_date
}
#Uncomment the lines below to delete the dates from the file
#col_to_remove <- which(names(data) == "Date")
#data <- data[-c(col_to_remove)]
write.table(data, file=file_name, quote=FALSE, sep='\t', row.names = FALSE)
#Return data so user can preview in R
return(data)
}
file <- 'RCL-IDH-029.txt'
#Make sure you are in the directory where your files are stored
#The first parameter is the name of the file
#The second paramter is the first date the patient was tested
#If the patient hasn't been used in TEV yet, leave as NULL and the earliest date in the file
#Will be used as timepoint 0
#Otherwise supply earliest date used in TEV in the format 'm/d/yy', i.e '5/14/16'
data <- date_to_day(file, NULL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment