Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/newercsvs 5.17")
- filenames = list.files(pattern="*.csv") #check folder for all csv's and make a list of file paths
- tmp1<-strsplit(filenames, "(?=[A-Za-z])(?<=[0-9])|(?=[0-9])(?<=[A-Za-z])", perl=TRUE)
- tmp2 <- do.call("rbind",tmp1)
- tmp2[,3]<-unlist(strsplit(tmp2[,3],split=".csv"))
- subs<-tmp2[,2:3]
- subs
- for(i in 1:length(filenames)){
- setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/newercsvs 5.17")
- print(i)
- if(subs[i,2]=="A"){
- print("A")
- dat<- read.csv(filenames[i]) #read in individual csv
- dat2<-subset(dat, select=c(SCC_EventNumber,Time,
- VDS_Veh_Speed,CFS_Accelerator_Pedal_Position,
- CFS_Brake_Pedal_Force,VDS_Eyepoint_Pos_1,VDS_Eyepoint_Pos_2))
- dat2$subnum<-subs[i,1]
- dat2$condition<-subs[i,2]
- ############ crossing #1 heading east
- dat3 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -7950 & dat2$VDS_Eyepoint_Pos_1 < -4950 &
- dat2$VDS_Eyepoint_Pos_2 > 1625 & dat2$VDS_Eyepoint_Pos_2 < 1665) , ]
- xdiff<-c(NA)
- for(j in 1:(nrow(dat3)-1)){
- xdiff[j]<-dat3$VDS_Eyepoint_Pos_1[j+1]-dat3$VDS_Eyepoint_Pos_1[j]
- }
- splitat<-which(abs(xdiff)>100)
- dat4<-dat3[1:splitat[1],]
- dat5<-dat3[(splitat[1]+1):splitat[2],]
- dat6<-dat3[(splitat[2]+1):nrow(dat3),]
- ############ crossing #2 heading west
- dat7 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -4950 & dat2$VDS_Eyepoint_Pos_1 < -1960 &
- dat2$VDS_Eyepoint_Pos_2 > -2975 & dat2$VDS_Eyepoint_Pos_2 < -2955) , ]
- xdiff2<-c(NA)
- for(k in 1:(nrow(dat7)-1)){
- xdiff2[k]<-dat7$VDS_Eyepoint_Pos_1[k+1]-dat7$VDS_Eyepoint_Pos_1[k]
- }
- splitat2<-which(abs(xdiff2)>100)
- dat8<-dat7[1:splitat2[1],]
- dat9<-dat7[(splitat2[1]+1):splitat2[2],]
- dat10<-dat7[(splitat2[2]+1):nrow(dat7),]
- setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/output")
- write.csv(dat4, file = paste0(filenames[i],"E1",".csv"),row.names=F)
- write.csv(dat5, file = paste0(filenames[i],"E3",".csv"),row.names=F)
- write.csv(dat6, file = paste0(filenames[i],"E5",".csv"),row.names=F)
- write.csv(dat8, file = paste0(filenames[i],"E2",".csv"),row.names=F)
- write.csv(dat9, file = paste0(filenames[i],"E4",".csv"),row.names=F)
- write.csv(dat10, file = paste0(filenames[i],"E6",".csv"),row.names=F)
- }
- ################################
- if(subs[i,2]=="B"){
- print("B")
- dat<- read.csv(filenames[i]) #read in individual csv
- dat2<-subset(dat, select=c(SCC_EventNumber,Time,
- VDS_Veh_Speed,CFS_Accelerator_Pedal_Position,
- CFS_Brake_Pedal_Force,VDS_Eyepoint_Pos_1,VDS_Eyepoint_Pos_2))
- dat2$subnum<-subs[i,1]
- dat2$condition<-subs[i,2]
- ############ crossing #1 heading east
- dat3 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -7950 & dat2$VDS_Eyepoint_Pos_1 < -4950 &
- dat2$VDS_Eyepoint_Pos_2 > 1625 & dat2$VDS_Eyepoint_Pos_2 < 1665) , ]
- xdiff<-c(NA)
- for(j in 1:(nrow(dat3)-1)){
- xdiff[j]<-dat3$VDS_Eyepoint_Pos_1[j+1]-dat3$VDS_Eyepoint_Pos_1[j]
- }
- splitat<-which(abs(xdiff)>100)
- dat4<-dat3[1:splitat[1],]
- dat5<-dat3[(splitat[1]+1):splitat[2],]
- dat6<-dat3[(splitat[2]+1):nrow(dat3),]
- ############ crossing #2 heading west
- dat7 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -4950 & dat2$VDS_Eyepoint_Pos_1 < -1960 &
- dat2$VDS_Eyepoint_Pos_2 > -2975 & dat2$VDS_Eyepoint_Pos_2 < -2955) , ]
- xdiff2<-c(NA)
- for(k in 1:(nrow(dat7)-1)){
- xdiff2[k]<-dat7$VDS_Eyepoint_Pos_1[k+1]-dat7$VDS_Eyepoint_Pos_1[k]
- }
- splitat2<-which(abs(xdiff2)>100)
- dat8<-dat7[1:splitat2[1],]
- dat9<-dat7[(splitat2[1]+1):splitat2[2],]
- dat10<-dat7[(splitat2[2]+1):nrow(dat7),]
- setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/output")
- write.csv(dat4, file = paste0(filenames[i],"E1",".csv"),row.names=F)
- write.csv(dat5, file = paste0(filenames[i],"E3",".csv"),row.names=F)
- write.csv(dat6, file = paste0(filenames[i],"E5",".csv"),row.names=F)
- write.csv(dat8, file = paste0(filenames[i],"E2",".csv"),row.names=F)
- write.csv(dat9, file = paste0(filenames[i],"E4",".csv"),row.names=F)
- write.csv(dat10, file = paste0(filenames[i],"E6",".csv"),row.names=F)
- }
- ################################
- if(subs[i,2]=="C"){
- print("C")
- dat<- read.csv(filenames[i]) #read in individual csv
- dat2<-subset(dat, select=c(SCC_EventNumber,Time,
- VDS_Veh_Speed,CFS_Accelerator_Pedal_Position,
- CFS_Brake_Pedal_Force,VDS_Eyepoint_Pos_1,VDS_Eyepoint_Pos_2))
- dat2$subnum<-subs[i,1]
- dat2$condition<-subs[i,2]
- ############ crossing #1 heading east
- dat3 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -8880 & dat2$VDS_Eyepoint_Pos_1 < -5880 &
- dat2$VDS_Eyepoint_Pos_2 > 1625 & dat2$VDS_Eyepoint_Pos_2 < 1665) , ]
- xdiff<-c(NA)
- for(j in 1:(nrow(dat3)-1)){
- xdiff[j]<-dat3$VDS_Eyepoint_Pos_1[j+1]-dat3$VDS_Eyepoint_Pos_1[j]
- }
- splitat<-which(abs(xdiff)>100)
- dat4<-dat3[1:splitat[1],]
- dat5<-dat3[(splitat[1]+1):splitat[2],]
- dat6<-dat3[(splitat[2]+1):nrow(dat3),]
- ############ crossing #2 heading west
- dat7 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -6550 & dat2$VDS_Eyepoint_Pos_1 < -3550 &
- dat2$VDS_Eyepoint_Pos_2 < -2200 & dat2$VDS_Eyepoint_Pos_2 > -2400) , ]
- xdiff2<-c(NA)
- for(k in 1:(nrow(dat7)-1)){
- xdiff2[k]<-dat7$VDS_Eyepoint_Pos_1[k+1]-dat7$VDS_Eyepoint_Pos_1[k]
- }
- splitat2<-which(abs(xdiff2)>100)
- dat8<-dat7[1:splitat2[1],]
- dat9<-dat7[(splitat2[1]+1):splitat2[2],]
- dat10<-dat7[(splitat2[2]+1):nrow(dat7),]
- setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/output")
- write.csv(dat4, file = paste0(filenames[i],"E1",".csv"),row.names=F)
- write.csv(dat5, file = paste0(filenames[i],"E3",".csv"),row.names=F)
- write.csv(dat6, file = paste0(filenames[i],"E5",".csv"),row.names=F)
- write.csv(dat8, file = paste0(filenames[i],"E2",".csv"),row.names=F)
- write.csv(dat9, file = paste0(filenames[i],"E4",".csv"),row.names=F)
- write.csv(dat10, file = paste0(filenames[i],"E6",".csv"),row.names=F)
- }
- }
Add Comment
Please, Sign In to add comment