Skip to content

Instantly share code, notes, and snippets.

@aa989190f363e46d
Created October 13, 2014 21:53
Show Gist options
  • Save aa989190f363e46d/ce94b37415f64005bee1 to your computer and use it in GitHub Desktop.
Save aa989190f363e46d/ce94b37415f64005bee1 to your computer and use it in GitHub Desktop.
GCDCourseProject
DATA_ROOT < 'UCI HAR Dataset'
curr.sep <- .Platform[['file.sep']]
read.commons <- function(){
act.labels <<- read.table(paste(DATA_ROOT
,'activity_labels.txt'
, sep=curr.sep)
,sep=' '
,col.names=c('index','name'))
col.names <- read.table(paste(DATA_ROOT
,'features.txt'
, sep=curr.sep)
,stringsAsFactors=F
,sep=' ')[[2]]
selected.cols <<- grepl('-mean()|-std()',col.names)
# pretty.col.names <<- gsub('([[:lower:]])([[:upper:]])'
# , '\\1 \\2'
# , gsub( '[.-]|\\(\\)'
# , ' '
# , col.names[selected.cols]))
pretty.col.names <<- col.names[selected.cols]
}
read.datasets <- function(dataset.name){
y.data <- read.table(paste(DATA_ROOT
, dataset.name
, paste0('y_',dataset.name,'.txt')
, sep=curr.sep
)
)[[1]]
y.data <- act.labels[y.data,2]
subj.data <- read.table(paste(DATA_ROOT
, dataset.name
, paste0('subject_',dataset.name,'.txt')
, sep=curr.sep
)
)[[1]]
x.data <- read.table(text=gsub( '[[:space:]]+'
, ';'
, sub( '^[[:space:]]+'
, ''
, readLines(paste( DATA_ROOT
, dataset.name
, paste0('X_',dataset.name,'.txt')
, sep=curr.sep)
)
)
)
, sep=';'
)[,selected.cols]
cbind(as.factor(subj.data),y.data,x.data)
}
read.commons()
result.df <- rbind(read.datasets('test'),read.datasets('train'))
names(result.df) <- c('subject','activity',pretty.col.names)
agg <- aggregate(. ~ subject + activity, data = result.df, mean)
write.table(agg,paste(DATA_ROOT,'output.txt',sep=curr.sep),sep=';',col.names=names(agg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment