Last active
September 29, 2015 08:40
-
-
Save cwhy/ca9142fd520e99f33634 to your computer and use it in GitHub Desktop.
CWhy's R starter file
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
rm(list = ls()) | |
# Set SPSS-like contrast | |
options(contrasts=c(unordered="contr.sum", ordered="contr.poly")) | |
# plug-in operators | |
`%+%` <- function(a, b) paste(a, b, sep="") | |
`%beginwith%` <- function(s.main, s.sub){ | |
bl <- nchar(s.sub) | |
return(substr(s.main,1,bl)==s.sub) | |
} | |
`%endwith%` <- function(s.main, s.sub){ | |
bl <- nchar(s.sub) | |
al <- nchar(s.main) | |
return(substr(s.main,al-bl+1,al)==s.sub) | |
} | |
`%notin%` <- Negate('%in%') | |
# Save Nested list as csv files | |
save_r <- function(the_list, the_name, include, exclude) { | |
print(the_name) | |
if(hasArg(include)){ | |
cols <- include | |
}else{ | |
if(hasArg(exclude)) | |
cols <- names(the_list) %notin% exclude | |
else | |
cols <- names(the_list) | |
} | |
if (is.null(names(the_list))) | |
names(the_list) <- as.character(1:length(the_list)) | |
if (is.data.frame(the_list)){ | |
str(the_list[cols], list.len = 3) | |
write.csv(the_list[cols], the_name %+% '.csv', row.names = F) | |
return | |
} | |
for (i in 1:length(the_list)) { | |
the_item <- the_list[[i]] | |
if (is.list(the_item)){ | |
full_name <- paste(the_name, names(the_list)[i], sep='.') | |
save_r(the_item, full_name, include, exclude) | |
} | |
} | |
} | |
# Read CSV files from folder | |
dat.file.names <- list.files(pattern='*.csv') | |
dat.files <- lapply(dat.file.names, read.csv) | |
remove.extension <- function(s){ | |
s.split <- strsplit(s, split='[.]')[[1]] | |
return(s.split[-length(s.split)]) | |
} | |
names(dat.files) <- lapply(dat.file.names, remove.extension) | |
# dataframe helpers | |
replace.colname <- function(df, old.name, new.name) { | |
colnames(df)[names(df) == old.name] <- new.name | |
return(df) | |
} | |
factorize.df <- function(df, factor.list){ | |
for (f in factor.list){ | |
df[[f]] <- as.factor(df[[f]]) | |
} | |
return(df) | |
} | |
#GGboxplot.dummy | |
ggplot(aes(y=value), data=t.dat) + | |
geom_boxplot(aes(x=phase, fill=session)) + | |
# xlab(' ') + | |
ylab(var) + | |
theme(legend.position="bottom") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment