Skip to content

Instantly share code, notes, and snippets.

# SC state transition data play
library(tidyverse)
library(readr)
data <- tibble(read_csv("12376.csv")) %>%
select(timestamp, TimeStamp,Speed,ACC_State,HOLCA_State,HOLCA_Icon,StrgWhlTchSnsHndsOnStat,DMSIP_DrvrAttnStatAuth,Take_Steering_Msg,SWLB = StrgWhlLgtBarCmdLgtBrStat) %>%
mutate_all(as.numeric) %>%
mutate(Speed = Speed*2.237) #Converts meters per second to MPH
@sglandry
sglandry / aggregating.r
Created July 16, 2020 18:33
Aggregating large number of CSV's into 1 data set, summarizing each file down to 1 line, then merging with other dataset based on common variable
library(tidyverse)
library(pbapply)
wd <- getwd() # get working directory
setwd(file.path(wd,"pupil detection")) # look in emotions_processed folder
files <- list.files() # 176 files
# read in and combine all files
allclips <- pblapply(files,function(x){
read_csv(x) %>%
# plot.me accepts an argument of a specific question title (Q) and analyzes then plots all the subquestions (speed and steering Q's for the same image)
# example usage: plot.me('14') to analyze questions that start with '14.'
plot.me <- function(Q){
data %>%
select(starts_with(Q),pauser2) %>%
rename(speed = ends_with(".2"), steer = ends_with(".3")) %>%
pivot_longer(cols = c(speed,steer),names_to = "system") %>%
group_by(pauser2,system) %>%
@sglandry
sglandry / saveplotstopdf.r
Created October 24, 2019 20:49
Saving multiple plots to a .pdf
library(progress)
pb <- progress_bar$new(
format = " Synchronizing with the matrix bleep blorp [:bar] :percent eta: :eta",
total = length(unique(data$trip_id)), clear = FALSE, width= 60)
pdf("plots.pdf", onefile = TRUE)
for(trip in unique(data$trip_id)){
pb$tick()
theplot <- data %>%
filter(trip_id == trip) %>%
@sglandry
sglandry / anthonycombine.r
Last active October 24, 2019 20:37
better combining files
library(tidyverse);library(readxl);library(pbapply)
trips <- read_csv('//ctl-apett.mit.edu/agelab_data/AVT/trips.csv') %>%
mutate(trip_id=as.character(trip_id)) %>%
select(trip_id,vehicle_type,trip_label_share)
files.cpap <- list.files('//ctl-apett.mit.edu/agelab_data/AVT/annotation_projects/aa_cp_annotations/annotations','xlsx',full.names = T,recursive = T) %>%
enframe(NULL,'path') %>%
filter(!str_detect(path,'template|Copy|~|/_'))
@sglandry
sglandry / regstudy.r
Created March 15, 2019 12:27
regstudy 3.15
library(readr)
library(ggplot2)
library("reshape2", lib.loc="~/R/win-library/3.3")
library(googlesheets)
library(httpuv)
library(dplyr)
library(lme4)
library(lmerTest)
library("lme4", lib.loc="~/R/win-library/3.3")
setwd("C:/Users/sglan/Desktop/Dissertation stats")
@sglandry
sglandry / emostudy.r
Last active March 15, 2019 12:28
Emo study 3.15
library(readr)
library(ggplot2)
library("reshape2", lib.loc="~/R/win-library/3.3")
library(googlesheets)
library(httpuv)
library(dplyr)
library("lme4", lib.loc="~/R/win-library/3.3")
setwd("C:/Users/sglan/Desktop/Dissertation stats")
get.se <- function(y) {
@sglandry
sglandry / Volvo.r
Created February 4, 2019 18:58
Volvo Interview Analysis
library(readxl)
library(stringr)
library(reshape2)
library(ggplot2)
library(readr)
# Because these data are essentially quotations attached to lists of codes,
# a function `list.to.matrix` was created to take a list with vector items of
# different lengths and create a matrix with the same length of the list but to
# have a width equal to the length of the longest vector item to accommodate all
@sglandry
sglandry / ALL NDS vs SIM data clean analysis.r
Created June 21, 2018 02:44
ALL NDS vs SIM data clean analysis
library(ggplot2)
library(reshape2)
setwd("M:/mtucifs_home/Desktop/Trains/Study 2")
list.files()
SIMA1<- read.csv("M:/mtucifs_home/Desktop/Trains/Study 2/SIMA1.csv")
SIMA2<- read.csv("M:/mtucifs_home/Desktop/Trains/Study 2/SIMA2.csv")
SIMC1<- read.csv("M:/mtucifs_home/Desktop/Trains/Study 2/SIMC1.csv")
@sglandry
sglandry / HRDdatacombo.r
Last active May 31, 2018 13:06
HRD data combining and splitting
path<-"//homedir.mtu.edu/home/Desktop/Trains/Study 2/newest/HRD Data/" ### home path
setwd(path)
subfolders<-dir()
path2<-"//homedir.mtu.edu/home/Desktop/Trains/Study 2/newest/cleaned/" ### destination folder for cleaned up HRD data
path3<-"//homedir.mtu.edu/home/Desktop/Trains/Study 2/newest/Newest (all)/" ### source folder for all DAQ CSV's
path4<-"//homedir.mtu.edu/home/Desktop/Trains/Study 2/newest/cleanmerged/" ### destination folder for merged files
path5<-"//homedir.mtu.edu/home/Desktop/Trains/Study 2/newest/splitbyevents/" ##### destination folder for final merged files split by event
# loop through each sub folder
# loop through each test folder