Created
October 22, 2013 20:12
-
-
Save alexstorer/7107318 to your computer and use it in GitHub Desktop.
A few files for doing data management in R. They're a little sloppy, but they work on what we tested!
This file contains hidden or 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
| install.packages('rjson') | |
| library(rjson) | |
| help(package=rjson) | |
| orig.data <- read.csv('/Users/astorer/Work/lgermine/gradCPT_results_8-13-13.csv',stringsAsFactors=FALSE) | |
| #install.packages('RJSONIO') | |
| library(RJSONIO) | |
| getDataFromJson <- function(charstr) { | |
| foobar <- RJSONIO::fromJSON(charstr,simplify=TRUE) | |
| all.data <- data.frame(rbind(foobar[[1]],foobar[[2]]),stringsAsFactors=FALSE) | |
| for (i in 3:length(foobar)) { | |
| all.data <- rbind(all.data,foobar[[i]]) | |
| } | |
| return(all.data) | |
| } | |
| dat.total <- data.frame() | |
| for (i in 1:nrow(orig.data)) { | |
| dat.df <- getDataFromJson(orig.data[1,'data']) | |
| dat.meta <- orig.data[1,] | |
| dat.meta$data <- NULL | |
| dat.merged <- merge(dat.df,dat.meta) | |
| dat.total <- rbind(dat.total,dat.merged) | |
| } |
This file contains hidden or 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
| # Step 1: Find location of all CSV files | |
| # Change this to the directory containing your data | |
| datadir <- '~/Work/lgermine/' | |
| all.files <- list.files(path=datadir,pattern='\\.csv$',recursive=T) | |
| # Step 2: Read them in and append the subject id | |
| # Step 3: Merge them | |
| all.data <- data.frame() | |
| for (fname in all.files) { | |
| subjectid <- strsplit(basename(fname),split='.',fixed=T)[[1]][1] | |
| subj.data <- read.csv(paste0(datadir,fname)) | |
| subj.data$subjectid <- subjectid | |
| print(paste("Reading for subject:",subjectid)) | |
| result = tryCatch({ | |
| all.data <- rbind(all.data,subj.data) | |
| }, error = function(e) { | |
| print(paste("Something went wrong for",subjectid)) | |
| }) | |
| } | |
| subset.data <- all.data[all.data$trial==1,] | |
| write.csv(subset.data,'foo.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment