back.R 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. library(dplyr)
  2. path = file(file.choose())
  3. file = read.csv(path, stringsAsFactors = FALSE, header = T, sep = ",")
  4. axis = data.frame(
  5. axis = character(),
  6. stringsAsFactors = FALSE
  7. )
  8. xAxis = "X"
  9. yAxis = "Y"
  10. zAxis = "Z"
  11. x = 1
  12. y = 2
  13. z = 3
  14. for (i in 1:(nrow(file))) {
  15. if (i / z == 1) {
  16. z = i + 3
  17. axis[i,1] = zAxis
  18. }else if(i / y == 1){
  19. y = i + 3
  20. axis[i,1] = yAxis
  21. }else if(i / x == 1){
  22. x = i + 3
  23. axis[i,1] = xAxis
  24. }
  25. }
  26. fileWithAxis = cbind(file, axis)
  27. angleBody = select(fileWithAxis,Time, axis,SpineBase, Head)
  28. rm(xAxis,yAxis,zAxis, x, y , z, file,axis, fileWithAxis)
  29. valueBetweenTwoValue = data.frame(
  30. Time = integer(),
  31. Difference = double(),
  32. stringsAsFactors = FALSE
  33. )
  34. for (i in 1:nrow(angleBody)) {
  35. valueBetweenTwoValue[i,1] = angleBody[i,1]
  36. valueBetweenTwoValue[i,2] = angleBody[i,3] - angleBody[i,4]
  37. }
  38. valueBetweenTwoValue = abs(valueBetweenTwoValue)
  39. calculateAngle = data.frame(
  40. Time = integer(),
  41. Angle = double(),
  42. stringsAsFactors = FALSE
  43. )
  44. doCalculateAngle = function(x){
  45. O = x[1]
  46. A = x[3]
  47. S = sqrt((O^2)+(A^2))
  48. newA = x[2]
  49. radian = atan(S/newA)
  50. degrees = radian*(180/pi)
  51. return(degrees)
  52. }
  53. initialTime = valueBetweenTwoValue[1,1]
  54. count = 1;
  55. timeDivision = 10^7
  56. for(i in seq(from=1, to=nrow(valueBetweenTwoValue), by=3)){
  57. calculateAngle[count,1] = (valueBetweenTwoValue[i,1] - initialTime)/timeDivision
  58. vector = valueBetweenTwoValue$Difference[i:(i+2)]
  59. calculateAngle[count,2] = doCalculateAngle(vector)
  60. count = count + 1
  61. }
  62. plot(calculateAngle$Time,calculateAngle$Angle, xlab = "Second", ylab = "Degrees", main = "Angele of body", type = "l")
  63. View(calculateAngle)
  64. healthyPercentage = 20
  65. if(mean(calculateAngle[, 2]) > healthyPercentage){
  66. print("High risk")
  67. }else{
  68. print("Low risk")
  69. }