title: Analysing entire dataframes instead of separate subsets in r
body: <p>I have code that analyses Gforces in movement, that returns to me a plot of GForces over time. The script works for subsets of the data (categorized by ID) and I don't have a clue how to make it compatible for analyzing the entire dataframe at once, returning to me the results in a faceted graph (with ggplot for instance). Does anyone know how to do this or am i doomed to analyse my data by one ID at a time?</p>
  
  <p>The code is:</p>
  
  <pre><code>#Subset by ID
  number1 &lt;-subset(positions,subset=(ID==1))
  head(number1)
  
  A &lt;- numeric()
  Al &lt;- numeric()
  Radius &lt;- numeric()
  GForce &lt;- numeric()
  D12 &lt;- numeric()
  D13 &lt;- numeric()
  D23 &lt;- numeric()
  Proportion &lt;- numeric()
  Proportion_sel &lt;- numeric()
  
  nr&lt;-length(number1$Timestamp)
  for(i in 3:nr){
  
    D12[i] &lt;- sqrt((positions$X[i-2]-positions$X[i-1])^2 + (positions$Y[i-2]-positions$Y[i-1])^2)
    D23[i] &lt;- sqrt((positions$X[i-1]-positions$X[i])^2 + (positions$Y[i-1]-positions$Y[i])^2)
    D13[i] &lt;- sqrt((positions$X[i-2]-positions$X[i])^2 + (positions$Y[i-2]-positions$Y[i])^2)
  
    if (D13[i]&gt;0) {
      if ((((D12[i]^2+D13[i]^2-D23[i]^2)^2)/(4*(D13[i]^2))) &gt; D12[i]^2) {
        A[i] &lt;- 0
        Radius[i] &lt;- 0
        GForce[i] &lt;- 0
      } 
      else {
         A[i] &lt;- sqrt(D12[i]^2-(((D12[i]^2+D13[i]^2-D23[i]^2)^2)/(4*(D13[i]^2))))
        Radius[i] &lt;- ((0.5*D13[i])^2+(A[i])^2)/(2*A[i])
        GForce[i] &lt;- ((D12[i]+D23[i])/2)^2/Radius[i]
      }
    }
    else {
      A[i] &lt;- 0
  Radius[i] &lt;- 0
      GForce[i] &lt;- 0
    } 
  
  }
  
  # GForce plot over Time
  plot(number1$Timestamp,GForce)
  </code></pre>
  
  <p>This returns to me:
  - a plot of Gforces over time  </p>
  
  <p>The problem seems to arise with finding a way to return the GForce per ID and return them as an additional column to the original dataframe. When this would be possible, it would also be easy to plot per ID using ggplot but I don't know how to accomplish this..</p>
  
  <p>Data (simplified):</p>
  
  <pre><code>    dput(positions)
  structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
  ), Timestamp = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
  12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
  25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
  38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 1L, 2L, 3L, 4L, 5L, 6L, 
  7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
  20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 
  33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L
  ), X = c(-34.126, -34.087, -34.047, -34.01, -33.983, -33.973, 
  -33.985, -34.019, -34.07, -34.13, -34.188, -34.237, -34.273, 
  -34.295, -34.305, -34.307, -34.305, -34.301, -34.299, -34.297, 
  -34.296, -34.29, -34.272, -34.235, -34.178, -34.107, -34.034, 
  -33.967, -33.91, -33.865, -33.832, -33.81, -33.799, -33.8, -33.814, 
  -33.841, -33.878, -33.923, -33.975, -34.033, -34.098, -34.17, 
  -34.243, -34.311, -34.366, -33.691, -33.646, -33.598, -33.547, 
  -33.497, -33.452, -33.414, -33.383, -33.357, -33.331, -33.302, 
  -33.268, -33.234, -33.203, -33.179, -33.163, -33.154, -33.145, 
  -33.132, -33.111, -33.081, -33.041, -32.993, -32.937, -32.873, 
  -32.807, -32.74, -32.676, -32.611, -32.543, -32.468, -32.384, 
  -32.293, -32.199, -32.109, -32.029, -31.959, -31.899, -31.846, 
  -31.796, -31.749, -31.704, -31.659, -31.612, -31.561), Y = c(3.393, 
  3.396, 3.398, 3.402, 3.408, 3.419, 3.434, 3.452, 3.471, 3.489, 
  3.506, 3.518, 3.525, 3.526, 3.523, 3.518, 3.513, 3.511, 3.511, 
  3.513, 3.516, 3.517, 3.51, 3.494, 3.467, 3.434, 3.402, 3.376, 
  3.358, 3.348, 3.343, 3.343, 3.346, 3.351, 3.36, 3.373, 3.39, 
  3.408, 3.43, 3.453, 3.479, 3.506, 3.532, 3.555, 3.57, 6.684, 
  6.757, 6.823, 6.887, 6.953, 7.024, 7.099, 7.174, 7.245, 7.307, 
  7.363, 7.414, 7.466, 7.52, 7.579, 7.643, 7.71, 7.781, 7.853, 
  7.923, 7.988, 8.047, 8.098, 8.139, 8.173, 8.202, 8.228, 8.251, 
  8.27, 8.285, 8.298, 8.314, 8.337, 8.372, 8.423, 8.487, 8.558, 
  8.628, 8.688, 8.735, 8.768, 8.792, 8.814, 8.843, 8.881)), .Names = c("ID", 
  "Timestamp", "X", "Y"), row.names = c(NA, 90L), class = "data.frame")
  </code></pre>