Guest User

Untitled

a guest
May 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/newercsvs 5.17")
  2. filenames = list.files(pattern="*.csv") #check folder for all csv's and make a list of file paths
  3.  
  4.  
  5. tmp1<-strsplit(filenames, "(?=[A-Za-z])(?<=[0-9])|(?=[0-9])(?<=[A-Za-z])", perl=TRUE)
  6. tmp2 <- do.call("rbind",tmp1)
  7. tmp2[,3]<-unlist(strsplit(tmp2[,3],split=".csv"))
  8. subs<-tmp2[,2:3]
  9. subs
  10.  
  11. for(i in 1:length(filenames)){
  12. setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/newercsvs 5.17")
  13. print(i)
  14. if(subs[i,2]=="A"){
  15. print("A")
  16. dat<- read.csv(filenames[i]) #read in individual csv
  17. dat2<-subset(dat, select=c(SCC_EventNumber,Time,
  18. VDS_Veh_Speed,CFS_Accelerator_Pedal_Position,
  19. CFS_Brake_Pedal_Force,VDS_Eyepoint_Pos_1,VDS_Eyepoint_Pos_2))
  20. dat2$subnum<-subs[i,1]
  21. dat2$condition<-subs[i,2]
  22.  
  23. ############ crossing #1 heading east
  24. dat3 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -7950 & dat2$VDS_Eyepoint_Pos_1 < -4950 &
  25. dat2$VDS_Eyepoint_Pos_2 > 1625 & dat2$VDS_Eyepoint_Pos_2 < 1665) , ]
  26.  
  27. xdiff<-c(NA)
  28. for(j in 1:(nrow(dat3)-1)){
  29. xdiff[j]<-dat3$VDS_Eyepoint_Pos_1[j+1]-dat3$VDS_Eyepoint_Pos_1[j]
  30. }
  31.  
  32. splitat<-which(abs(xdiff)>100)
  33. dat4<-dat3[1:splitat[1],]
  34. dat5<-dat3[(splitat[1]+1):splitat[2],]
  35. dat6<-dat3[(splitat[2]+1):nrow(dat3),]
  36.  
  37.  
  38. ############ crossing #2 heading west
  39. dat7 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -4950 & dat2$VDS_Eyepoint_Pos_1 < -1960 &
  40. dat2$VDS_Eyepoint_Pos_2 > -2975 & dat2$VDS_Eyepoint_Pos_2 < -2955) , ]
  41.  
  42. xdiff2<-c(NA)
  43. for(k in 1:(nrow(dat7)-1)){
  44. xdiff2[k]<-dat7$VDS_Eyepoint_Pos_1[k+1]-dat7$VDS_Eyepoint_Pos_1[k]
  45. }
  46.  
  47. splitat2<-which(abs(xdiff2)>100)
  48. dat8<-dat7[1:splitat2[1],]
  49. dat9<-dat7[(splitat2[1]+1):splitat2[2],]
  50. dat10<-dat7[(splitat2[2]+1):nrow(dat7),]
  51.  
  52.  
  53. setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/output")
  54. write.csv(dat4, file = paste0(filenames[i],"E1",".csv"),row.names=F)
  55. write.csv(dat5, file = paste0(filenames[i],"E3",".csv"),row.names=F)
  56. write.csv(dat6, file = paste0(filenames[i],"E5",".csv"),row.names=F)
  57.  
  58. write.csv(dat8, file = paste0(filenames[i],"E2",".csv"),row.names=F)
  59. write.csv(dat9, file = paste0(filenames[i],"E4",".csv"),row.names=F)
  60. write.csv(dat10, file = paste0(filenames[i],"E6",".csv"),row.names=F)
  61. }
  62.  
  63. ################################
  64. if(subs[i,2]=="B"){
  65. print("B")
  66. dat<- read.csv(filenames[i]) #read in individual csv
  67. dat2<-subset(dat, select=c(SCC_EventNumber,Time,
  68. VDS_Veh_Speed,CFS_Accelerator_Pedal_Position,
  69. CFS_Brake_Pedal_Force,VDS_Eyepoint_Pos_1,VDS_Eyepoint_Pos_2))
  70. dat2$subnum<-subs[i,1]
  71. dat2$condition<-subs[i,2]
  72.  
  73. ############ crossing #1 heading east
  74. dat3 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -7950 & dat2$VDS_Eyepoint_Pos_1 < -4950 &
  75. dat2$VDS_Eyepoint_Pos_2 > 1625 & dat2$VDS_Eyepoint_Pos_2 < 1665) , ]
  76.  
  77. xdiff<-c(NA)
  78. for(j in 1:(nrow(dat3)-1)){
  79. xdiff[j]<-dat3$VDS_Eyepoint_Pos_1[j+1]-dat3$VDS_Eyepoint_Pos_1[j]
  80. }
  81.  
  82. splitat<-which(abs(xdiff)>100)
  83. dat4<-dat3[1:splitat[1],]
  84. dat5<-dat3[(splitat[1]+1):splitat[2],]
  85. dat6<-dat3[(splitat[2]+1):nrow(dat3),]
  86.  
  87.  
  88. ############ crossing #2 heading west
  89. dat7 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -4950 & dat2$VDS_Eyepoint_Pos_1 < -1960 &
  90. dat2$VDS_Eyepoint_Pos_2 > -2975 & dat2$VDS_Eyepoint_Pos_2 < -2955) , ]
  91.  
  92. xdiff2<-c(NA)
  93. for(k in 1:(nrow(dat7)-1)){
  94. xdiff2[k]<-dat7$VDS_Eyepoint_Pos_1[k+1]-dat7$VDS_Eyepoint_Pos_1[k]
  95. }
  96.  
  97. splitat2<-which(abs(xdiff2)>100)
  98. dat8<-dat7[1:splitat2[1],]
  99. dat9<-dat7[(splitat2[1]+1):splitat2[2],]
  100. dat10<-dat7[(splitat2[2]+1):nrow(dat7),]
  101.  
  102. setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/output")
  103. write.csv(dat4, file = paste0(filenames[i],"E1",".csv"),row.names=F)
  104. write.csv(dat5, file = paste0(filenames[i],"E3",".csv"),row.names=F)
  105. write.csv(dat6, file = paste0(filenames[i],"E5",".csv"),row.names=F)
  106.  
  107. write.csv(dat8, file = paste0(filenames[i],"E2",".csv"),row.names=F)
  108. write.csv(dat9, file = paste0(filenames[i],"E4",".csv"),row.names=F)
  109. write.csv(dat10, file = paste0(filenames[i],"E6",".csv"),row.names=F)
  110. }
  111.  
  112. ################################
  113. if(subs[i,2]=="C"){
  114. print("C")
  115. dat<- read.csv(filenames[i]) #read in individual csv
  116. dat2<-subset(dat, select=c(SCC_EventNumber,Time,
  117. VDS_Veh_Speed,CFS_Accelerator_Pedal_Position,
  118. CFS_Brake_Pedal_Force,VDS_Eyepoint_Pos_1,VDS_Eyepoint_Pos_2))
  119. dat2$subnum<-subs[i,1]
  120. dat2$condition<-subs[i,2]
  121.  
  122. ############ crossing #1 heading east
  123. dat3 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -8880 & dat2$VDS_Eyepoint_Pos_1 < -5880 &
  124. dat2$VDS_Eyepoint_Pos_2 > 1625 & dat2$VDS_Eyepoint_Pos_2 < 1665) , ]
  125.  
  126. xdiff<-c(NA)
  127. for(j in 1:(nrow(dat3)-1)){
  128. xdiff[j]<-dat3$VDS_Eyepoint_Pos_1[j+1]-dat3$VDS_Eyepoint_Pos_1[j]
  129. }
  130.  
  131. splitat<-which(abs(xdiff)>100)
  132. dat4<-dat3[1:splitat[1],]
  133. dat5<-dat3[(splitat[1]+1):splitat[2],]
  134. dat6<-dat3[(splitat[2]+1):nrow(dat3),]
  135.  
  136.  
  137. ############ crossing #2 heading west
  138. dat7 <- dat2[ which( dat2$VDS_Eyepoint_Pos_1 > -6550 & dat2$VDS_Eyepoint_Pos_1 < -3550 &
  139. dat2$VDS_Eyepoint_Pos_2 < -2200 & dat2$VDS_Eyepoint_Pos_2 > -2400) , ]
  140.  
  141. xdiff2<-c(NA)
  142. for(k in 1:(nrow(dat7)-1)){
  143. xdiff2[k]<-dat7$VDS_Eyepoint_Pos_1[k+1]-dat7$VDS_Eyepoint_Pos_1[k]
  144. }
  145.  
  146. splitat2<-which(abs(xdiff2)>100)
  147. dat8<-dat7[1:splitat2[1],]
  148. dat9<-dat7[(splitat2[1]+1):splitat2[2],]
  149. dat10<-dat7[(splitat2[2]+1):nrow(dat7),]
  150.  
  151. setwd("//homedir.mtu.edu/home/Desktop/Trains/Study 1/newercsvs 5.17/output")
  152. write.csv(dat4, file = paste0(filenames[i],"E1",".csv"),row.names=F)
  153. write.csv(dat5, file = paste0(filenames[i],"E3",".csv"),row.names=F)
  154. write.csv(dat6, file = paste0(filenames[i],"E5",".csv"),row.names=F)
  155.  
  156. write.csv(dat8, file = paste0(filenames[i],"E2",".csv"),row.names=F)
  157. write.csv(dat9, file = paste0(filenames[i],"E4",".csv"),row.names=F)
  158. write.csv(dat10, file = paste0(filenames[i],"E6",".csv"),row.names=F)
  159. }
  160.  
  161. }
Add Comment
Please, Sign In to add comment